标签:des style blog http color io os ar java
Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1095 Accepted Submission(s): 226
1 //#define LOCAL 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 using namespace std; 7 const int inf=0x3f3f3f3f; 8 const int maxn=250; 9 int city[maxn][maxn],lowc[maxn]; 10 char str[10050]; 11 bool vis[maxn]; 12 int Q,n,t,k; 13 int d[3]; 14 int Distra(int st,int en,int type) 15 { 16 int i,j; 17 memset(vis,‘\0‘,sizeof(vis)); 18 memset(lowc,0,sizeof(lowc)); 19 vis[st]=1; 20 for(i=1;i<=n;i++){ 21 if(city[st][i]>=type) 22 lowc[i]=1; 23 else 24 lowc[i]=inf; 25 } 26 lowc[st]=0; //原点为0 27 int pre=st,minc; 28 for(i=1;i<n;i++) 29 { 30 minc=inf; 31 for(j=1;j<=n;j++){ 32 if(!vis[j]&&city[pre][j]>=type&&1+lowc[pre]<=lowc[j]) 33 { 34 lowc[j]=lowc[pre]+1; 35 } 36 } 37 for(j=1; j<=n ;j++) 38 { 39 if(!vis[j]&&lowc[j]<minc) 40 { 41 minc=lowc[j]; 42 pre=j; 43 } 44 } 45 vis[pre]=1; 46 } 47 if(lowc[en]==inf)lowc[en]=0; 48 return lowc[en]; 49 } 50 int main() 51 { 52 int val,fr,a,b; 53 bool flag; 54 #ifdef LOCAL 55 freopen("test.in","r",stdin); 56 #endif 57 scanf("%d",&Q); 58 while(Q--){ 59 scanf("%d%d",&n,&t); 60 memset(city,0,sizeof(city)); 61 while(t--){ 62 scanf("%s %d",str,&k); 63 fr=val=0; 64 flag=0; 65 for(int i=0; ;i++) 66 { 67 if(str[i]==‘\0‘)str[i]=‘+‘,flag=1; 68 if(str[i]!=‘+‘) 69 val=val*10+str[i]-‘0‘; 70 else 71 { 72 if(fr==0) fr=val; 73 else 74 { 75 if(city[fr][val]<k+1) 76 city[fr][val]=k+1; 77 fr=val; 78 } 79 val=0; 80 } 81 if(flag) break; 82 } 83 } 84 scanf("%d%d",&d[1],&d[2]); 85 scanf("%d%d",&a,&b); 86 int lenb=Distra(a,b,1); //以硬座进行查找 87 if(lenb==0) 88 printf("-1\n"); 89 else 90 { 91 int lena=Distra(a,b,2); //以卧铺进行查找 92 if(lena==0) 93 printf("%d\n",lenb*d[1]); 94 else 95 printf("%d\n",min(lena*d[2],lenb*d[1])); 96 } 97 } 98 return 0; 99 }
一些测试数据:
4 6 5 2+4+3+5+1+6 1 5+4+2+3+1 1 3+2+5+1+6 1 6+2 0 6+3+1+4+5+2 0 3 2 /* 5 3 6 3 3+4 1 4+2+6 0 3+2+1+6 1 3 2 3 6 6 3 3+4 1 4+2+6 0 3+2+1+6 1 3 2 5 6 6 4 3+4 1 4+2+6 0 3+2+1+6 1 3+1+5+2+6 0 3 2 5 6 */ 分别为: 4 6 -1 6
hdu----(4522)湫湫系列故事——过年回家(最短路)
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/gongxijun/p/3995632.html