| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 10898 | Accepted: 4836 |
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 ≤ X ≤ N). 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?
Input
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
Hint
=========================
有向图,求在不同点的牛到指定点来回的最短路之和的最大值是多少
由于dijkstra是求单源最短路,所以需要把矩阵逆置一次算两次最短路
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=111;
const int maxm=111111;
const int INF=0x3f3f3f;
int n,m,x,MAX=-1;
int map[1111][1111],dis1[1111],dis2[1111],vis[1111];
void dijkstra(int s,int dis[])
{
for(int i=1; i<=n; i++){
dis[i]=INF;
vis[i]=false;
}
dis[s]=0;
for(int rp=1;rp<=n;rp++)
{
int u=0,mn=INF;
for (int i=1;i<=n;i++){
if (!vis[i]&&dis[i]<mn){
mn=dis[i];
u=i;
}
}
if(u==0) return;
vis[u]=true;
for(int i=1;i<=n;i++)
{
if(map[u][i]<INF&&dis[i]>dis[u]+map[u][i])
dis[i]=dis[u]+map[u][i];
}
}
}
void change()
{
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
swap(map[i][j],map[j][i]);
}
int main()
{
scanf("%d%d%d",&n,&m,&x);
memset(map,INF,sizeof(map));
int u,v,w;
for(int i=1;i<=n;i++)
map[i][i]=0;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
map[u][v]=w;
}
dijkstra(x,dis1);
change();
dijkstra(x,dis2);
for(int i=1;i<=n;i++)
{
MAX=max(dis1[i]+dis2[i],MAX);
}
printf("%d\n",MAX);
return 0;
}
牛聚会最短路径

本文介绍了一种利用Dijkstra算法解决有向图中各农场到指定聚会地点往返最短路径的问题,并通过实例演示了如何计算最长行走时间。
348

被折叠的 条评论
为什么被折叠?



