思路: 模板题,求解最短路的四种算法都可以。注意求解最短路时候要处理成无向图
代码:
/*Dijkstra*/ #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; #define MAXN 110 #define INF 0xFFFFFFF int n , m; int value[MAXN][MAXN]; int dis[MAXN]; void Dijkstra(){ int pos; int vis[MAXN]; memset(vis , 0 , sizeof(vis)); dis[1] = 0; for(int i = 2 ; i <= n ; i++) dis[i] = INF; for(int i = 1 ; i <= n ; i++){ pos = -1; for(int j = 1 ; j <= n ; j++){ if(!vis[j] && (pos == -1 || dis[j] < dis[pos])) pos = j; } vis[pos] = 1; for(int j = 1 ; j <= n ; j++){ if(!vis[j] && dis[j] > dis[pos] + value[pos][j]) dis[j] = dis[pos] + value[pos][j]; } } printf("%d\n" , dis[n]); } int main(){ //freopen("input.txt" , "r" , stdin); int star , end , v; while(scanf("%d%d" , &n , &m) , n+m){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++) value[i][j] = INF; } for(int i = 0 ; i < m ; i++){ scanf("%d%d%d" , &star , &end , &v); if(value[star][end] != INF) value[star][end] = value[end][star] = v; else{ if(value[star][end] > v) value[star][end] = value[end][star] = v; } } Dijkstra(); } return 0; } /*floyd*/ /*floyd*/ #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; #define MAXN 110 #define INF 0xFFFFFFF int n , m; long long dis[MAXN][MAXN]; void init(){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ if(i == j) dis[i][j] = 0; else dis[i][j] = INF; } } } long long min(int a , int b){ return a < b ? a : b; } void floyd(){ for(int k = 1 ; k <= n ; k++){ for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++) dis[i][j] = min(dis[i][k]+ dis[k][j] , dis[i][j]); } } printf("%lld\n" , dis[1][n]); } int main(){ //freopen("input.txt" , "r" , stdin); int a , b , value; while(scanf("%d%d" , &n , &m) , n+m){ init(); for(int i = 0 ; i < m ; i++){ scanf("%d%d%d" , &a , &b , &value); dis[a][b] = dis[b][a] = value; } floyd(); } return 0; } /*Bellman_Ford*/ #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; #define MAXN 20010 #define INF 0xFFFFFFF int n , m; long long dis[MAXN]; struct Edge{ int x; int y; int value; }e[MAXN]; void Bellman_Ford(){ dis[1] = 0; for(int i = 2 ; i <= n ; i++) dis[i] = INF; for(int i = 1 ; i < n ; i++){ for(int j = 0 ; j < 2*m ; j++){ if(dis[e[j].y] > dis[e[j].x] + e[j].value) dis[e[j].y] = dis[e[j].x] + e[j].value; } } printf("%lld\n" , dis[n]); } int main(){ //freopen("input.txt" , "r" , stdin); while(scanf("%d%d", &n , &m) , n+m){ for(int i = 0 ; i < m ; i++){ scanf("%d%d%d" , &e[i].x , &e[i].y , &e[i].value); e[i+m].x = e[i].y; e[i+m].y = e[i].x; e[i+m].value= e[i].value; } Bellman_Ford(); } return 0; } /*SPFA*/ #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define MAXN 20010 #define INF 0xFFFFFFF int n , m; int first[MAXN] , next[MAXN]; int star[MAXN] , end[MAXN] , value[MAXN]; int dis[MAXN]; int vis[MAXN]; queue<int>q; void init(){ memset(first , -1 , sizeof(first)); memset(next , -1 , sizeof(next)); memset(vis , 0 , sizeof(vis)); } void SPFA(){ dis[1] = 0; for(int i = 2 ; i <= n ; i++) dis[i] = INF; vis[1] = 1; while(!q.empty()) q.pop(); q.push(1); while(!q.empty()){ int tmp = q.front(); q.pop(); vis[tmp] = 0; for(int i = first[tmp] ; i != -1 ; i = next[i]){ if(dis[end[i]] > dis[tmp] + value[i]){ dis[end[i]] = dis[tmp ] + value[i]; if(!vis[end[i]]){ vis[end[i]] = 1; q.push(end[i]); } } } } printf("%d\n" , dis[n]); } int main(){ //freopen("input.txt" , "r" , stdin); while(scanf("%d%d" , &n , &m) , n+m){ init(); /*处理成无向图*/ for(int i = 0 ; i < m ;i++){ scanf("%d%d%d" , &star[i] , &end[i] , &value[i]); star[i+m] = end[i]; end[i+m] = star[i]; value[i+m] = value[i]; next[i] = first[star[i]]; first[star[i]] = i; next[i+m] = first[star[i+m]]; first[star[i+m]] = i+m; } SPFA(); } return 0; }
时间: 2024-09-15 16:34:26