zoj 3166 Lazy Tourist(最短路Floyd)

求出到达hotel的环的最短路。

 

因为是求最后再到达hotel的最小环, 所以初始化都为INT_MAX

 

单向的路,我很纠结 = =。。。

 

其他就很水了。

 

#include <stdio.h> #include <stdlib.h> #include <iostream> #include <memory.h> #include <limits.h> using namespace std; int main(void) { int map[110][110]; int n,c,m,from,to,len; int hotel[110]; while( cin >> n >> c ) { for(int i=0; i<=n; i++) for(int k=0; k<=n; k++) map[i][k] = INT_MAX; for(int i=1; i<=c; i++) cin >> hotel[i]; cin >> m; int sum = INT_MAX; for(int i=0; i<m; i++) { cin >> from >> to >> len; map[from][to] = len; //map[to][from] = len is wrong. Single way. } for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) for(int k=1; k<=n; k++) if( map[j][i] != INT_MAX && map[i][k] != INT_MAX && map[j][k] > map[j][i] + map[i][k] ) map[j][k] = map[j][i] + map[i][k]; int result = 0; for(int i=1; i<=c; i++) if( map[hotel[i]][hotel[i]] < sum ) // Find the smallest one. { sum = map[hotel[i]][hotel[i]]; result = hotel[i]; } if( result ) cout << result << endl; else cout << "I will nerver go to that city!" << endl; } return 0; }  

你可能感兴趣的:(zoj 3166 Lazy Tourist(最短路Floyd))