题目连接。
解题:
题中的起始点为0(假设元素从0开始),终止点为n-1
Dijkstra:
View Code
//起始为0,终点为n-1 #include <stdio.h> #include <limits.h> #include <string.h> #define MAXN 102 #define INF (1<<20) //如果很小的话。是不能AC的。 int v[MAXN], d[MAXN], w[MAXN][MAXN]; void Dijkstra(int n){ memset(v, 0, sizeof(v)); int i, y; for(i=0; i<n; i++) d[i] = (i == 0 ? 0 : INF); for(i=0; i<n; i++){ int x, m = INF; for(y=0; y<n; y++) if(!v[y] && d[y]<=m) m = d[x = y]; v[x] = 1; for(y=0; y<n; y++){ if(!v[y] && d[y] > m + w[x][y]) d[y] = d[x] + w[x][y]; } } } int main(){ int n, m, a, b, c, i, j; while(scanf("%d %d", &n, &m) == 2 && (n != 0 || m != 0)){ for(i=0; i<n; i++){ for(j=0; j<n; j++) w[i][j] = INF; } for(i=0; i<m; i++){ scanf("%d %d %d", &a, &b, &c); w[a-1][b-1] = w[b-1][a-1] = c; } Dijkstra(n); printf("%d\n", d[n-1]); } return 0; }
如果要修改起始点的话。只要修改一下Dijkstra函数就OK了
View Code
void Dijkstra(int v0, int n){ //v0为起始点 memset(v, 0, sizeof(v)); int i, y; for(i=0; i<n; i++) d[i] = w[v0][i]; d[v0]=0; v[0] = 1; for(i=0; i<n; i++){ int x, m = INF; for(y=0; y<n; y++) if(!v[y] && d[y]<=m) m = d[x = y]; v[x] = 1; for(y=0; y<n; y++){ if(!v[y] && d[y] > m + w[x][y]) d[y] = d[x] + w[x][y]; } } }
Spfa:
View Code
#include <cstdio> #include <queue> using namespace std; #define MAXN 102 const int INF=(1<<24); int n, m, d[MAXN], w[MAXN][MAXN], f[MAXN]; //f为判断当前点是否在队列中 void spfa(int v0){ //v0为起点 queue<int> q; int i; for(i=0; i<n; i++){ d[i] = INF; f[i] = 0; } d[v0] = 0; f[v0] = 1; q.push(v0); while(!q.empty()){ int t = q.front(); f[t] = 0; q.pop(); for(i=0; i<n; i++){ if(w[t][i]<INF && d[t]+w[t][i] < d[i]){ d[i] = d[t]+w[t][i]; if(f[i] == 0){ f[i] = 1; q.push(i); } } } } } int main(){ int i,j; while(scanf("%d %d", &n, &m) == 2 && (n != 0 || m != 0)){ for(i=0; i<n; i++) for(j=0; j<n; j++) w[i][j] = INF; for(i=0; i<m; i++){ int a, b, c; scanf("%d %d %d", &a, &b, &c); a--; b--; //序号从0开始 w[a][b] = w[b][a] = c; } spfa(0); printf("%d\n", d[n-1]); } return 0; }