标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 864 Accepted Submission(s): 329
1 #include<stdio.h> 2 #include<string.h> 3 #define MAX(x,y)(x>y?x:y) 4 const int INF=0x3f3f3f3f; 5 const int MAXN=40; 6 int N; 7 int d[MAXN],vis[MAXN]; 8 int map[MAXN][MAXN],num[MAXN]; 9 void initial(){ 10 for(int i=1;i<=N;i++){ 11 d[i]=-INF; 12 } 13 memset(vis,0,sizeof(vis)); 14 } 15 struct Node{ 16 int s,e,dis; 17 }; 18 Node dt[1010]; 19 void dijskra(int s){ 20 initial(); 21 d[s]=0; 22 while(true){ 23 int k=-1; 24 for(int i=1;i<=N;i++) 25 if(!vis[i]&&(k==-1||d[i]>d[k]))k=i; 26 if(k==-1)break; 27 vis[k]=1; 28 for(int i=1;i<=N;i++) 29 d[i]=MAX(d[i],d[k]+map[k][i]); 30 } 31 } 32 int main(){ 33 int M,a,b,c; 34 while(~scanf("%d%d",&N,&M),N||M){ 35 for(int i=0;i<M;i++){ 36 scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].dis); 37 }int temp=0,t,ans=1,answer=0; 38 for(t=2;t<N;t++){ 39 for(int i=1;i<=N;i++){ 40 for(int j=1;j<=N;j++){ 41 map[i][j]=-INF; 42 } 43 } 44 for(int i=0;i<M;i++){ 45 map[dt[i].s][dt[i].e]=map[dt[i].e][dt[i].s]=dt[i].dis; 46 } 47 // printf("t=%d\n",t); 48 for(int i=1;i<=N;i++){ 49 map[t][i]=map[i][t]=-INF; 50 } 51 dijskra(1); 52 if(d[N]==-INF){ 53 ans=0; 54 break; 55 } 56 answer=MAX(answer,d[N]); 57 } 58 if(ans)printf("%d\n",answer); 59 else puts("Inf"); 60 } 61 return 0;}
How Many Maos Does the Guanxi Worth
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4737924.html