Bellman无向图
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=500;
struct edge{
int head;
int tail;
int cost;
}edge[N];
int graph[N][N];
int check[N];
int dis[N];
int n,m;
void Bellman()
{
int i,p,j;
int check;
for(i=1;i<=n;i++)
dis[i]=(i==1?0:INF);
for(i=1;i<=n-1;i++)
{
check=0;
for(j=1;j<=2*m;j++)
if(dis[edge[j].tail]>dis[edge[j].head]+edge[j].cost)
{
dis[edge[j].tail]=dis[edge[j].head]+edge[j].cost;
check=1;
}
if(check==0)
break;
}
/*此处应添加判断负权环的代码*/
/*此处应添加判断负权环的代码*/
/*此处应添加判断负权环的代码*/
/*此处应添加判断负权环的代码*/
}
int main()
{
int i,p,j;
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
for(i=1;i<=2*m;i++)
{
scanf("%d%d%d",&edge[i].head,&edge[i].tail,&edge[i].cost);
i++;
edge[i].head=edge[i-1].tail;
edge[i].tail=edge[i-1].head;
edge[i].cost=edge[i-1].cost;
}
Bellman();
printf("%d\n",dis[n]);
}
return 0;
}