#include <stdio.h>#include <queue>#include <iostream>using namespace std;#define INF 0xfffff //因为为了辨别是否有负权,所以INF不能开太大#define MAX 1100int dist[MAX], pre[MAX], path[MAX][MAX];bool sign[MAX];void initialize(int n) //初始化{ for(int i=1; i<=n; i++) { { //pre[i] = 0; dist[i] = INF; //将距离开始全变为最大 sign[i] = false; } for(int j=1; j<=n; j++) path[i][j] = INF; //图初始 }}void spfa(int n, int start) //无法计算负权,对边较多的更快速{ /* for (int i=1; i<=n; ++i)//初始化 { dist[i] = INF; sign[i] = false; }*/ queue<int> Q; dist[start] = 0; sign[start] = true; Q.push(start); while (!Q.empty()){ int temp = Q.front(); Q.pop(); for (int i=0; i<=n; ++i) { if (dist[temp] + path[temp][i] < dist[i])//存在负权的话,就需要创建一个COUNT数组,当某点的入队次数超过V(顶点数)返回。 { dist[i] = dist[temp] + path[temp][i]; if (!sign[i]) { Q.push(i); sign[i] = true; } //这个内循环可以判断所要权值相对应条件的值如dist[start]; } } sign[temp] = false; } }void floyd (int n) //边较为少而稀疏,能一次性求出所有的顶点到顶点的最短路径{ int i, j, k; for (k = 1; k <= n; k++) for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) if (path[i][k] + path[k][j] < path[i][j]) path[i][j] = path[i][k] + path[k][j]; } void input(int line){ int a, b, weight; for(int i=0; i<line; i++) { scanf("%d%d%d", &a, &b, &weight); if(path[a][b] > weight) //有多条路,保存最短的那条 { path[a][b] = weight; path[b][a] = weight; //无向图双向 } }}int main(){ int n, line; scanf("%d%d", &n, &line); initialize(n); input(line); spfa(n, 1); printf("%d
", dist[n]); return 0; }