标签:
题意:题如其名,给你n个单位 ,以及n-1个单位换算信息,让你把完整的单位换算链写出来。
解题思路:搜索 或者 floyd
解题代码:
1 // File Name: j.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月14日 星期六 15时56分31秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 26 using namespace std; 27 char str[20][1000]; 28 int mp[12][12]; 29 char tmp1[1000]; 30 char tmp2[1000]; 31 int tmp ; 32 char c; 33 int n; 34 struct node{ 35 int si,v; 36 }ans[1100]; 37 int cmp(node a, node b) 38 { 39 return a.v < b.v; 40 } 41 int main(){ 42 while(scanf("%d",&n) != EOF) 43 { 44 if(n == 0) 45 break; 46 memset(mp,0,sizeof(mp)); 47 for(int i = 1;i <= n;i ++) 48 { 49 scanf("%s",str[i]); 50 } 51 int mx = 0 ; 52 int mxnum = 0; 53 for(int i = 1;i <= n-1;i ++) 54 { 55 scanf("%s %c %d %s",tmp1,&c,&tmp,tmp2); 56 int ta; 57 int tb; 58 for(int j = 1;j <= n;j++) 59 { 60 if(strcmp(str[j],tmp1) == 0 ) 61 { 62 ta = j ; 63 break; 64 } 65 } 66 for(int j= 1;j <= n;j ++) 67 { 68 if(strcmp(str[j],tmp2) == 0 ) 69 { 70 tb = j ; 71 break; 72 } 73 } 74 mp[ta][tb] = tmp; 75 } 76 int tt = 1; 77 while(tt--) 78 { 79 for(int k = 1;k <= n ;k ++) 80 { 81 for(int i = 1;i <= n ;i ++) 82 for(int j= 1;j <= n ;j ++) 83 { 84 if(mp[i][k] != 0 && mp[k][j] != 0 ) 85 { 86 mp[i][j] = mp[i][k] *mp[k][j]; 87 } 88 if(mp[i][k] != 0 && mp[j][k] != 0 ) 89 { 90 if(mp[i][k] > mp[j][k]) 91 { 92 mp[i][j] = mp[i][k]/mp[j][k]; 93 }else{ 94 mp[j][i] = mp[j][k]/mp[i][k]; 95 } 96 } 97 if(mp[k][i] != 0 && mp[k][j] != 0 ) 98 { 99 if(mp[k][i] > mp[k][j]) 100 mp[j][i] = mp[k][i]/mp[k][j]; 101 else 102 mp[i][j] = mp[k][j]/mp[k][i]; 103 } 104 if(mp[i][j] > mx) 105 { 106 mx = mp[i][j]; 107 mxnum = i; 108 } 109 } 110 } 111 } 112 for(int i = 1;i <=n;i ++) 113 { 114 ans[i].si = i; 115 ans[i].v = mp[mxnum][i]; 116 } 117 sort(ans+1,ans+1+n,cmp); 118 printf("1%s",str[mxnum]); 119 for(int i = 2;i <= n;i ++) 120 printf(" = %d%s",ans[i].v,str[ans[i].si]); 121 printf("\n"); 122 } 123 124 return 0; 125 }
ACM-ICPC North America Qualifier 2014 Units
标签:
原文地址:http://www.cnblogs.com/zyue/p/4338308.html