6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1
50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
spfa一开始把函数写外边老WA.....
dijkstra:
#include<cstdio> #include<cstring> #include<cmath> #define Wi(a) while(a--) #define Si(a) scanf("%d", &a) #define Sch(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define mem(a, b) memset(a, (b), sizeof(a)) #define INF 0x3f3f3f #include<algorithm> using namespace std; const int mx = 200; int map[mx][mx], dis[mx], vis[mx]; char now[33], to[33], str[mx][33]; int n, N; int s, t; void init() { for(int i = 0; i < mx; i++) { for(int j = 0; j < mx; j++) map[i][j] = i==j?0:INF; } } int turn(char s[]) { for(int i = 1; i <= n; i++) { if(strcmp(s, str[i])==0) return i; } strcpy(str[++n], s); return n; } void dijkstra(int s) { mem(vis, 0); int i, j; for(i = 1; i <= n; i++) dis[i] = map[s][i]; vis[s] = 1; for(i = 1; i < n; i++) { int minn = INF, k = s; for(j = 1; j <= n; j++) { if(!vis[j] && dis[j] < minn) { minn = dis[j]; k = j; } } vis[k] = 1; for(j = 1; j <= n; j++) { if(!vis[j]) dis[j] = min(dis[j], dis[k]+map[k][j]); } } if(dis[t] != INF) Pi(dis[t]); else if(s == t) puts("0"); else puts("-1"); } int main(){ while(Si(N)==1, N != -1) { init(); n = 0; Sch(now); s = turn(now); Sch(to); t = turn(to); int a, b, c; Wi(N) { Sch(now); Sch(to); Si(c); a = turn(now); b = turn(to); if(map[a][b] > c) map[a][b] = map[b][a] = c; } dijkstra(s); } return 0; }
floyd:
#include<cstdio> #include<cstring> #include<cmath> #define Wi(a) while(a--) #define Si(a) scanf("%d", &a) #define Sch(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define mem(a, b) memset(a, (b), sizeof(a)) #define INF 0x3f3f3f #include<algorithm> using namespace std; const int mx = 200; int map[mx][mx], dis[mx], vis[mx]; char now[33], to[33], str[mx][33]; int n, N; int s, t; void init() { for(int i = 0; i < mx; i++) { for(int j = 0; j < mx; j++) map[i][j] = i==j?0:INF; } } int turn(char *s) { for(int i = 1; i <= n; i++) { if(strcmp(s, str[i])==0) return i; } strcpy(str[++n], s); return n; } void floyd() { int i, j, k; for(k = 0; k <= n; k++){ for(i = 0; i <= n; i++){ for(j = 0; j <= n; j++) map[i][j] = min(map[i][j], map[i][k]+map[k][j]); } } if(map[s][t] != INF) Pi(map[s][t]); else if(s == t) puts("0"); else puts("-1"); } int main(){ while(Si(N)==1, N != -1) { init(); n = 0; Sch(now); s = turn(now); Sch(to); t = turn(to); int a, b, c; Wi(N) { Sch(now); Sch(to); Si(c); a = turn(now); b = turn(to); if(map[a][b] > c) map[a][b] = map[b][a] = c; } floyd(); } return 0; }
#include<cstdio> #include<cstring> #include<cmath> #include<queue> #define Wi(a) while(a--) #define Si(a) scanf("%d", &a) #define Sch(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define mem(a, b) memset(a, (b), sizeof(a)) #define INF 0x3f3f3f3f #include<algorithm> using namespace std; const int mx = 1010; int dis[mx], vis[mx], head[mx]; int edgenum, n, m; char str[mx][mx]; int find(char ch[]) { for(int i = 1; i <= n; i++) { if(strcmp(ch, str[i])==0) return i; } strcpy(str[++n], ch); return n; } struct node{ int from, to, val, next; }; node edge[211000]; void init() { n = 0; edgenum = 0; mem(str, '\0'); mem(head, -1); } void add(int a, int b, int c) { node E = {a, b, c, head[a] }; edge[edgenum] = E; head[a] = edgenum++; } void spfa(int s, int t) { queue<int> q; mem(dis, INF); mem(vis, 0); dis[s] = 0; vis[s] = 1; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].to; if(dis[v] > dis[u]+edge[i].val){ dis[v] = dis[u]+edge[i].val; if(!vis[v]){ vis[v] = 1; q.push(v); } } } } if(dis[t] != INF) Pi(dis[t]); else printf("-1\n"); } char s1[33],s2[33]; void getmap() { int a, b, c; Wi(m) { Sch(s1); a = find(s1); Sch(s2); b = find(s2); Si(c); add(a, b, c); add(b, a, c); } } int main(){ while(Si(m)==1, m!=-1) { init(); Sch(s1); int ss = find(s1); Sch(s2); int tt = find(s2); getmap(); spfa(ss, tt); } return 0; }