HDU Today
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19995 Accepted Submission(s): 4724
Problem Description
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
Input
输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);
第二行有徐总的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
note:一组数据中地名数不会超过150个。
如果N==-1,表示输入结束。
Output
如果徐总能到达目的地,输出最短的时间;否则,输出“-1”。
Sample Input
6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1
Sample Output
50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
/*dijkstra*/ #include<stdio.h> #include<string.h> #define INF 0x3f3f3f #define min(a, b) a>b?b:a using namespace std; const int mx = 160; int map[mx][mx],d[mx],vis[mx]; int n,start,end; char str[mx][33]; int find(char a[]) { for(int i = 1; i <= n; i ++) if(strcmp(a,str[i]) == 0) return i; strcpy(str[++n], a); return n; } void dijkstra(int s) { int i,j; for(i = 1; i <= n; i++) { vis[i] = 0; d[i] = map[s][i]; } vis[s] = 1; for( i = 1; i < n; i++) { int min = INF,k = s; for( j = 1; j <= n; j++) if(!vis[j] && d[j] < min) { min = d[j]; k = j; } vis[k] = 1; for( j = 1; j <= n; j++) if(!vis[j]) d[j] = min(d[j], d[k]+map[k][j]); } if(d[end]!=INF) printf("%d\n",d[end]); else if(start == end) printf("0\n"); else printf("-1\n"); } int main() { int i,j,k,c; char s[33],e[33]; while(scanf("%d",&c)!=EOF,c!=-1) { n = 0; int a,b,t; scanf("%s",s); start = find(s); scanf("%s",s); end = find(s); for(i = 0; i < 150; i++) for(j = 0; j < 150; j++) map[i][j] = INF; while(c--) { scanf("%s",s); a = find(s); scanf("%s",s); b = find(s); scanf("%d",&t); if(map[a][b] > t) map[a][b] = map[b][a] = t; } dijkstra(start); } } /*spfa*/ #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; const int mx =1600; int dis[mx],vis[mx],head[mx],edgenum; char str[mx][mx]; int start, ed,n;//为什么用end老是CE..... int find(char s[]) { for(int i = 1; i <= n;i++) if(strcmp(s,str[i])==0) return i; strcpy(str[++n],s); return n; } struct node{ int a,b,val,next; }p[211000]; void add(int u,int v,int w) { node E = {u, v, w, head[u]}; p[edgenum] = E; head[u]=edgenum++; } void spfa(int sx,int en) { queue<int> q; memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[sx] = 0; vis[sx] = 1; q.push(sx); while(!q.empty()) { int u = q.front(); q.pop();vis[u] = 0; for(int i = head[u]; i!=-1; i=p[i].next) { int v = p[i].b; if(dis[v] > dis[u]+p[i].val) { dis[v] = dis[u]+p[i].val; if(!vis[v]) { vis[v] = 1; q.push(v); } } } } if(dis[en] !=INF) printf("%d\n",dis[en]); else printf("-1\n"); } int main() { int i,j,k,c; char s[33],e[33]; while(scanf("%d",&c)!=EOF,c!=-1) { n = 0; edgenum = 0; memset(head, -1, sizeof(head)); memset(str, '\0', sizeof(str)); int a,b,t; scanf("%s",s); start = find(s); scanf("%s",e); ed = find(e); while(c--) { scanf("%s",s); a = find(s); scanf("%s",s); b = find(s); scanf("%d",&t); add(a,b,t); add(b,a,t); } spfa(start,ed); } return 0; } /*floyd*/ #include<stdio.h> #include<string.h> #define min(a,b) a>b?b:a #define INF 0x3f3f3f3f using namespace std; const int mx = 160; int dis[mx][mx]; char str[mx][33]; int n; int find(char a[]) { for(int i=1; i<=n; i++) if(strcmp(a,str[i])==0) return i; strcpy(str[++n],a); return n; } void floyd() { int i,j,k; for(k = 1; k <= n; k++) for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) dis[i][j] = min( dis[i][j], dis[i][k]+dis[k][j] ); } int main() { int i,j,k,c; char s[33],e[33]; while(scanf("%d",&c)!=EOF,c!=-1) { n = 0; memset(str,'\0',sizeof(str)); for(i = 0; i < 150; i++) for(j = 0; j < 150; j++) { if(i == j ) dis[i][j] = 0; else dis[i][j] = INF; } int s0,t0; int num = c; scanf("%s",s); s0 = find(s); scanf("%s",e); t0 = find(e); int q,z,t; while(c--) { scanf("%s", s); q = find(s); scanf("%s", s); z = find(s); scanf("%d", &t); if(dis[q][z] > t) dis[q][z] = dis[z][q] = t; } floyd(); if(dis[s0][t0]!=INF) printf("%d\n",dis[s0][t0]); else printf("-1\n"); } return 0; }
版权声明:原创文章,若要转载,请与博主联系,谢谢
时间: 2024-10-23 10:29:02