Bellman_Ford基本方法,和使用邻接表和队列的优化。由于没有负环而且最短路一定存在,所以不需要判负环那一步。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<sstream>
#define LL long long
#define OJ_DEBUG 0
#define READ_FILE 0
using namespace std;
const int NN_MAX = 110;
const int MM_MAX = NN_MAX*NN_MAX;
const int INF = 0x1fffffff;
struct Edge{
int a,b,c;
Edge (int a1=0,int b1=0,int c1=0):a(a1),b(b1),c(c1){}
bool operator < (const Edge& chs) const {return c<chs.c;}
}theEdge[MM_MAX];
/**********************************************************/
int n,m,s,t,lenEdge;
vector<int>G[NN_MAX];
int d[NN_MAX];
/**********************************************************/
int min_2 (int x,int y) {return x<y?x:y;}
int max_2 (int x,int y) {return x>y?x:y;}
void Bellman_Ford_1();
void Bellman_Ford_2();
/**********************************************************/
int main()
{
if (READ_FILE) freopen ("in.txt","r",stdin);
while( scanf("%d %d",&n,&m) && (n||m) )
{
lenEdge=0;
for(int i=0;i<=n;i++) G[i].clear();
int t1,t2,t3;
for(int i=0;i<m;i++){
scanf("%d %d %d",&t1,&t2,&t3);
theEdge[lenEdge++]=Edge(t1,t2,t3); G[t1].push_back(lenEdge-1);
theEdge[lenEdge++]=Edge(t2,t1,t3); G[t2].push_back(lenEdge-1);
}
s=1,t=n;
//Bellman_Ford_1();
Bellman_Ford_2();
printf("%d\n",d[t]);
}
return 0;
}
void Bellman_Ford_1()
{
for(int i=0;i<=n;i++) d[i]=INF;
d[s]=0;
for(int k=0;k<n-1;k++){
bool finish=true;
for(int i=0;i<lenEdge;i++){
int x=theEdge[i].a, y=theEdge[i].b;
if(d[x]<INF) d[y]=min_2(d[y],d[x]+theEdge[i].c);
finish=false;
}
if(finish) break;
}
}
void Bellman_Ford_2()
{
for(int i=0;i<=n;i++) d[i]=INF;
d[s]=0;
queue<int>qee;
qee.push(s);
while(!qee.empty())
{
int x=qee.front();qee.pop();
for(int i=0;i<G[x].size();i++){
Edge e=theEdge[G[x][i]];
if(d[x]<INF && d[e.b]>d[x]+e.c){
d[e.b]=d[x]+e.c;
qee.push(e.b);
}
}
}
}
Dijkstra基本方法,和使用邻接表和优先队列的优化。注意优先队列元素需要重载<
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<sstream>
#define LL long long
#define OJ_DEBUG 0
#define READ_FILE 1
using namespace std;
const int NN_MAX = 110;
const int MM_MAX = NN_MAX*NN_MAX;
const int INF = 0x1fffffff;
struct Edge{
int a,b,c;
Edge (int a1=0,int b1=0,int c1=0):a(a1),b(b1),c(c1){}
bool operator < (const Edge& chs) const {return c<chs.c;}
}theEdge[MM_MAX];
struct CostAndNode{
int d,b;
CostAndNode(int d1=0,int b1=0):d(d1),b(b1){}
bool operator < (const CostAndNode& chs) const {return d>chs.d;}
};
/**********************************************************/
int n,m,s,t,lenEdge;
vector<int>G[NN_MAX];
int d[NN_MAX],vis[NN_MAX],maps[NN_MAX][NN_MAX];
/**********************************************************/
int min_2 (int x,int y) {return x<y?x:y;}
int max_2 (int x,int y) {return x>y?x:y;}
void Dijkstra_1();
void Dijkstra_2();
/**********************************************************/
int main()
{
if (READ_FILE) freopen ("in.txt","r",stdin);
while( scanf("%d %d",&n,&m) && (n||m) )
{
lenEdge=0;
for(int i=0;i<=n;i++) G[i].clear();
int t1,t2,t3;
/*for(int i=0;i<=n;i++)
for(int j=i;j<=n;j++)
maps[i][j]=maps[j][i]=(i==j?0:INF);*/
for(int i=0;i<m;i++){
scanf("%d %d %d",&t1,&t2,&t3);
maps[t1][t2]=t3;
maps[t2][t1]=t3;
theEdge[lenEdge++]=Edge(t1,t2,t3); G[t1].push_back(lenEdge-1);
theEdge[lenEdge++]=Edge(t2,t1,t3); G[t2].push_back(lenEdge-1);
}
s=1,t=n;
//Dijkstra_1();
Dijkstra_2();
printf("%d\n",d[t]);
}
return 0;
}
void Dijkstra_1()
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++) d[i]=INF;
d[s]=0;
for(int k=0;k<n;k++){
int x=0;
for(int i=1;i<=n;i++)
if(!vis[i] && d[i]<d[x])
x=i;
vis[x]=1;
for(int y=1;y<=n;y++)
if(!vis[y])
d[y]=min_2(d[y],d[x]+maps[x][y]);
}
}
void Dijkstra_2()
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++) d[i]=INF;
d[s]=0;
priority_queue<CostAndNode>pq;
pq.push(CostAndNode(0,s));
while(!pq.empty())
{
CostAndNode cad=pq.top();pq.pop();
int x=cad.b;
if(vis[x]) continue;
vis[x]=1;
for(int i=0;i<G[x].size();i++){
Edge e=theEdge[G[x][i]];
if(!vis[e.b] && d[e.b]>d[x]+e.c){
d[e.b]=d[x]+e.c;
pq.push( CostAndNode(d[e.b],e.b) );
}
}
}
}
floyd也能过
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<sstream>
#define LL long long
#define OJ_DEBUG 0
#define READ_FILE 0
using namespace std;
const int NN_MAX = 110;
const int MM_MAX = NN_MAX*NN_MAX;
const int INF = 0x0fffffff;
/**********************************************************/
int n,m,s;
int maps[NN_MAX][NN_MAX];
/**********************************************************/
int min_2 (int x,int y) {return x<y?x:y;}
int max_2 (int x,int y) {return x>y?x:y;}
void floyd();
/**********************************************************/
int main()
{
if (READ_FILE) freopen ("in.txt","r",stdin);
while( scanf("%d %d",&n,&m) && (n||m) )
{
int t1,t2,t3;
for(int i=0;i<=n;i++)
for(int j=i;j<=n;j++)
maps[i][j]=maps[j][i]=(i==j?0:INF);
for(int i=0;i<m;i++){
scanf("%d %d %d",&t1,&t2,&t3);
maps[t1][t2]=t3;
maps[t2][t1]=t3;
}
floyd();
printf("%d\n",maps[1][n]);
}
return 0;
}
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(maps[i][k]<INF && maps[k][j]<INF)
maps[i][j]=min_2(maps[i][j],maps[i][k]+maps[k][j]);
}