标签:
额......最短路算法,从代码中去感悟吧,改变了下代码风格,不过感觉好别扭
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<queue> #include<map> #include<stack> #include<list> #include<vector> #include<cstring> #define inf 1<<30 #define Inf -1<<30 #define maxn 105 #define ll long long #define uli unsigned long int #define f_(i,x,n) for(int i=x;i<=n;i++) #define F_(i,x,n) for(int i=x;i>=n;i--) using namespace std; int n,m; vector<int>mapp[maxn]; int vaule[maxn][maxn]; void dijkstra() { int visit[maxn],z[maxn]; memset(visit,0,sizeof(visit)); fill(z+1,z+n+1,inf); z[1]=0; while(!visit[n]) { int v=-1; f_(i,1,n) { if(!visit[i]&&(v==-1||z[i]<z[v])) v=i; } visit[v]=1; f_(i,0,mapp[v].size()-1) { int x=mapp[v][i]; z[x]=min(z[x],vaule[v][x]+z[v]); } } cout<<z[n]<<endl; } int main() { while(cin>>n>>m) { if(!n&&!m) break; memset(vaule,0,sizeof(vaule)); f_(i,1,n) mapp[i].clear(); f_(i,1,m) { int x,y,z; cin>>x>>y>>z; mapp[x].push_back(y); mapp[y].push_back(x); vaule[x][y]=z; vaule[y][x]=z; } dijkstra(); } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/46476245