是一道最短路的题,先求目的点到个点的最短路,再把图反向,再求最短路即可。只是反向图的时候要注意一下,我就是这里犯错了,纠结了大概两个小时,,,题目:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7968 | Accepted: 3527 |
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
#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
const int N=1010;
#define MAX 1000000000
int map[N][N];
int pos,numpath,numcow;
int visted[N];
int flag[N][N];
void dijkstra(int s,int dis[N]){
memset(visted,0,sizeof(visted));
for(int i=0;i<=numcow;++i)
dis[i]=map[s][i];
dis[s]=0;
visted[s]=1;
int begin=s;
for(int i=1;i<=numcow;++i){
for(int j=1;j<=numcow;++j){
if(!visted[j]&&map[begin][j]!=MAX&&dis[begin]+map[begin][j]<dis[j]){
dis[j]=dis[begin]+map[begin][j];
}
}
int mmin=MAX;
for(int j=1;j<=numcow;++j){
if(!visted[j]&&dis[j]<mmin){
mmin=dis[j];
begin=j;
}
}
visted[begin]=1;
}
return ;
}
int main(){
// freopen("1.txt","r",stdin);
while(~scanf("%d%d%d",&numcow,&numpath,&pos)){
int x,y,z,dis1[N],dis2[N];
for(int i=0;i<=numcow;++i){
for(int j=0;j<=numcow;++j)
map[i][j]=MAX;
}
while(numpath--){
scanf("%d%d%d",&x,&y,&z);
if(z<map[x][y])
map[x][y]=z;
}
dijkstra(pos,dis1);
memset(flag,0,sizeof(flag));
for(int i=0;i<=numcow;++i){
for(int j=0;j<=numcow;++j){
if(map[i][j]!=MAX&&map[j][i]!=MAX&&!flag[i][j]){
int t=map[j][i];
map[j][i]=map[i][j];
map[i][j]=t;
flag[i][j]=1;flag[j][i]=1;
}
else if(map[i][j]!=MAX&&map[j][i]==MAX&&!flag[i][j]){
map[j][i]=map[i][j];
map[i][j]= MAX;
flag[i][j]=1;flag[j][i]=1;
}
else if(map[i][j]==MAX&&map[j][i]!=MAX&&!flag[j][i]){
map[i][j]=map[j][i];
map[j][i]=MAX;
flag[i][j]=1;flag[j][i]=1;
}
}
}
dijkstra(pos,dis2);
int count,mmax=0;
for(int i=1;i<=numcow;++i){
if(i==pos)
continue;
count=0;
count+=dis1[i];
count+=dis2[i];
if(count>mmax)
mmax=count;
}
printf("%d\n",mmax);
}
return 0;
}