标签:
Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1266 Accepted Submission(s): 282
///构造两个图,一个是卧铺,一个是硬座 #include<stdio.h> #include<string.h> #include <iostream> #include <algorithm> using namespace std; const int INF =999999999; const int N = 300; int graph[N][N],graph1[N][N]; ///graph 是卧铺,graph1是硬座 char str[10005]; int n,t,d1,d2; void init() { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(i==j) graph[i][j] = 0,graph1[i][j]=0; else graph[i][j] = INF,graph1[i][j]=INF; } } } bool vis[N]; int low[N]; int dijkstra(int s,int e,int g[][N]) { for(int i=1; i<=n; i++) { vis[i] = false; low[i] = g[s][i]; } vis[s] = true; for(int i=1; i<n; i++) { int MIN = INF; for(int j=1; j<=n; j++) { if(!vis[j]&&MIN>low[j]) { MIN = low[j]; s = j; } } vis[s] = true; for(int j=1; j<=n; j++) { if(!vis[j]&&low[j]>g[s][j]+low[s]) { low[j] = g[s][j]+low[s]; } } } return low[e]; } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%d%d",&n,&t); init(); int v; /*for(int i=1;i<=t;i++){ scanf("%s %d",str,&v); int len = strlen(str); int pre = str[0]-‘0‘,now; for(int j=2;j<len;j+=2){ ///错误代码!!!!!!我这样写比如说:‘12‘是取不到的!!!! now = str[j]-‘0‘; if(v==1){ graph[pre][now] = 1; graph1[pre][now] = 1; } else graph1[pre][now] = 1; ///为0时只构造graph1,graph不存在硬座 pre = now; } }*/ for(int i=1; i<=t; i++) { scanf("%s %d",str,&v); int len = strlen(str); int K; int pre = 0; int k = 0; while(str[k]!=‘+‘) { pre = pre*10+str[k]-‘0‘; k++; } for(int j=k+1; j<len; j++) { int now = 0; while(j<len&&str[j]!=‘+‘) { now = now*10+str[j]-‘0‘; j++; } if(v==1) { graph[pre][now] = 1; graph1[pre][now] = 1; } else graph1[pre][now] = 1; ///为0时只构造graph1,graph不存在硬座 pre = now; } } scanf("%d%d",&d1,&d2); int s,e; scanf("%d%d",&s,&e); int m1 = dijkstra(s,e,graph); ///卧铺的最小舒适度 int m2 = dijkstra(s,e,graph1); if(m1>=INF&&m2>=INF) printf("-1\n"); else if(m1>=INF) { printf("%d\n",m2*d1); } else if(m2>=INF) { printf("%d\n",m1*d2); } else printf("%d\n",min(m1*d2,m2*d1)); } return 0; } /* 6 4 3+4 1 4+2+6 0 3+2+1+6 1 3+1+5+2+6 0 3 2 5 6 */
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5618504.html