(简单) POJ 3268 Silver Cow Party,Dijkstra。

  Description

  One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

  Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

  Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

  正向边一次最短路,反向边一次就好了。。。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int INF=10e8;
const int MaxN=; struct Node
{
int v,val; Node(int _v=,int _val=):v(_v),val(_val) {}
bool operator < (const Node &a) const
{
return val>a.val;
}
}; struct Edge
{
int v,cost; Edge(int _v=,int _cost=):v(_v),cost(_cost) {}
}; vector <Edge> E[][MaxN]; void Dijkstra(int type,int lowcost[],int n,int start)
{
priority_queue <Node> que;
Node qtemp;
int u,v,c,len; for(int i=;i<=n;++i)
{
lowcost[i]=INF;
}
lowcost[start]=; que.push(Node(start,)); while(!que.empty())
{
qtemp=que.top();
que.pop(); u=qtemp.v; len=E[type][u].size(); for(int i=;i<len;++i)
{
v=E[type][u][i].v;
c=E[type][u][i].cost; if(lowcost[u]+c<lowcost[v])
{
lowcost[v]=lowcost[u]+c;
que.push(Node(v,lowcost[v]));
}
}
}
} inline void addEdge(int type,int u,int v,int c)
{
E[type][u].push_back(Edge(v,c));
} int ans1[MaxN],ans2[MaxN];
int maxans; int main()
{
int N,M,X;
int a,b,c; while(~scanf("%d %d %d",&N,&M,&X))
{
for(int i=;i<=M;++i)
{
scanf("%d %d %d",&a,&b,&c); addEdge(,a,b,c);
addEdge(,b,a,c);
} for(int i=;i<=N;++i)
ans1[i]=ans2[i]=;
maxans=-; Dijkstra(,ans1,N,X);
Dijkstra(,ans2,N,X); for(int i=;i<=N;++i)
if(ans1[i]+ans2[i]>maxans)
maxans=ans1[i]+ans2[i]; cout<<maxans<<endl;
} return ;
}
上一篇:机器学习实战knn


下一篇:Leetcode 27.移除元素 By Python