题意:从2~n-1这几个点中任意去掉一个点,使得从1到n的最短路径最大,如果任意去掉一个点1~n无通路输出Inf。
Sample Input
4 5
1 2 3
1 3 7
1 4 50
2 3 4
3 4 2
3 2
1 2 30
2 3 10
0 0
Sample Output
50
Inf
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <string> 6 # include <map> 7 # include <cmath> 8 # include <queue> 9 # include <list> 10 # define LL long long 11 using namespace std ; 12 13 const int MAXN=300; 14 const int INF=0x3f3f3f3f; 15 int n , m ; 16 bool vis[MAXN]; 17 int cost[MAXN][MAXN] ; 18 int lowcost[MAXN] ; 19 20 void Dijkstra(int beg) 21 { 22 for(int i=0;i<n;i++) 23 { 24 lowcost[i]=INF; 25 } 26 lowcost[beg]=0; 27 for(int j=0;j<n;j++) 28 { 29 int k=-1; 30 int Min=INF; 31 for(int i=0;i<n;i++) 32 if(!vis[i]&&lowcost[i]<Min) 33 { 34 Min=lowcost[i]; 35 k=i; 36 } 37 if(k==-1) 38 break ; 39 vis[k]=true; 40 for(int i=0;i<n;i++) 41 if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]) 42 { 43 lowcost[i]=lowcost[k]+cost[k][i]; 44 } 45 } 46 47 } 48 49 int main () 50 { 51 //freopen("in.txt","r",stdin) ; 52 while (scanf("%d %d" , &n , &m) !=EOF) 53 { 54 if (n == 0 && m == 0) 55 break ; 56 57 int u , v , w ; 58 int i , j ; 59 int MAX = 0 ; 60 for (i = 0 ; i < n ; i++) 61 for (j = 0 ; j < n ; j++) 62 { 63 if (i == j) 64 cost[i][j] = 0 ; 65 else 66 cost[i][j] = INF ; 67 } 68 while(m--) 69 { 70 scanf("%d%d%d" , &u , &v , &w) ; 71 cost[u-1][v-1] = w ; 72 cost[v-1][u-1] = w ; 73 74 } 75 76 for (i = 1 ; i <= n-2 ; i++) 77 { 78 memset(vis , 0 , sizeof(vis)) ; 79 vis[i] = 1 ; 80 Dijkstra(0) ; 81 if (lowcost[n-1] > MAX ) 82 MAX = lowcost[n-1] ; 83 84 } 85 86 if (MAX == INF) 87 printf("Inf\n") ; 88 else 89 printf("%d\n" , MAX) ; 90 } 91 92 return 0 ; 93 }
时间: 2024-10-19 10:47:28