点击打开链接hdu 1385
思路:最短路+SPFA+路径记录+路径字典序比较
分析:
1 题目要求的单源的最短路,所以可以选择任意一种单源最短路来求解
2 题目还要求在路径和相同情况下要字典序小的,那么就要在更新dis数组的时候进行更新路径,如果是dis[i]>tmp,那么直接更新;如果是dis[i] == tmp的情况下,那么就要求出star->i 和 star->x的路径进行比较,然后判断能否更新.
3 注意询问的时候可能问的是同一个点。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define MAXN 110 #define INF 0xFFFFFFF int n , star , end , pos; int value[MAXN][MAXN]; int tmp_value[MAXN][MAXN]; int dis[MAXN]; int charge[MAXN]; int vis[MAXN]; int route[MAXN]; int father[MAXN]; int tmp_charge[MAXN]; queue<int>q; /*初始化数组*/ void init(){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++) value[i][j] = INF; value[i][i] = 0; } } /*dfs找路径*/ void dfs(int num , char *str){ if(num == -1) return; dfs(father[num] , str); str[pos++] = num+'0'; } void SPFA(int s){ memset(vis , 0 , sizeof(vis)); for(int i = 1 ; i <= n ; i++){ dis[i] = INF; father[i] = -1; } vis[s] = 1; dis[s] = 0; q.push(s); while(!q.empty()){ int x = q.front(); q.pop(); vis[x] = 0; for(int i = 1 ; i <= n ; i++){ int tmp = dis[x] + tmp_value[x][i] + tmp_charge[x]; /*dis[i]>tmp直接更新*/ if(dis[i] > tmp){ dis[i] = tmp; father[i] = x; if(!vis[i]){ vis[i] = 1; q.push(i); } } /*dis[i] == tmp的时候求出路径*/ else if(dis[i] == tmp && x != i && dis[i] != INF){ char ch1[MAXN] = {'\0'} , ch2[MAXN] = {'\0'}; pos = 0; dfs(i , ch1); pos = 0; dfs(x , ch2); ch2[pos] = x+'0';/*这个地方注意就是x要加上去*/ /*如果字典序小就要更新*/ if(strcmp(ch1 , ch2) > 0){ father[i] = x; if(!vis[i]){ vis[i] = 1; q.push(i); } } } } } } int main(){ int x , y , num , cnt , ans; while(scanf("%d" , &n) && n){ init(); /*输入边*/ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ scanf("%d" , &num); if(num != -1 && value[i][j] > num) value[i][j] = num; } } /*输入过路费*/ for(int i = 1 ; i <= n ; i++) scanf("%d" , &charge[i]); /*询问*/ while(1){ scanf("%d%d" , &star , &end); if(star == -1 && end == -1) break; /*数组复制*/ memcpy(tmp_charge , charge , sizeof(charge)); memcpy(tmp_value , value , sizeof(value)); tmp_charge[star] = tmp_charge[end] = 0;/*起点和终点的过路费为0*/ SPFA(star); /*输出*/ printf("From %d to %d :\n" , star , end); printf("Path: "); memset(route , 0 , sizeof(route)); ans = dis[end]; if(star != end){ cnt = 0; x = end; while(1){ y = father[x]; route[cnt++] = y; if(y == star) break; x = y; } for(int i = cnt-1 ; i >= 0 ; i--) printf("%d-->" , route[i]); } printf("%d\n" , end); printf("Total cost : %d\n\n" , ans); } } return 0; }
2 floyd ,由于floyd是每一次都用k去更新dis,所以我们只要在更新的同时更新path即可,由于k是从1-n的,那么这样最后的path肯定是字典序最小的。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAXN 110 #define INF 0xFFFFFFF int n; int value[MAXN][MAXN]; int charge[MAXN]; int path[MAXN][MAXN]; void init(){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ value[i][j] = INF; path[i][j] = j; } value[i][i] = 0; } } void floyd(){ for(int k = 1 ; k <= n ; k++){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ int tmp = value[i][k]+value[k][j]+charge[k]; if(value[i][j] > tmp){ value[i][j] = tmp; path[i][j] = path[i][k]; } else if(value[i][j] == tmp && path[i][j] > path[i][k]) path[i][j] = path[i][k]; } } } } int main(){ int x , y , num , star , end; while(scanf("%d" , &n) && n){ init(); for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ scanf("%d" , &num); if(num != -1) value[i][j] = num; } } for(int i = 1 ; i <= n ; i++) scanf("%d" , &charge[i]); floyd(); while(1){ scanf("%d%d" , &star , &end); if(star == -1 && end == -1) break; printf("From %d to %d :\n" , star , end); printf("Path: %d" , star); x = star , y = end; if(star != end){ while(1){ int p = path[x][y]; printf("-->%d" , p); if(p == end) break; x = p; } } printf("\n"); printf("Total cost : %d\n\n" , value[star][end]); } } return 0; }