模板——Dijkstra

<div class="cnblogs_Highlighter">
<pre class="brush:cpp;gutter:true;">#include<bits/stdc++.h>
using namespace std;<br />int n,m,mp[1001][1001],s[1001];
bool judge[1001]={0};
//judge表示判断点是否有用过,s[i]表示1到点i的最短路径,mp表示两点的距离<br />int main(){
	cin>>n>>m;
	for (int i=1; i<=n; i++){
		for (int l=1; l<=n; l++){
			mp[i][l]=1e9;
		}
	}
	for (int i=1; i<=n; i++) s[i]=1e9;
	for (int i=1; i<=m; i++){
		int x,y,z;
		cin>>x>>y>>z;
		mp[x][y]=z;
	}
	for (int i=2; i<=n; i++) s[i]=mp[1][i];
	s[1]=0;
	judge[1]=true;
	for (int i=1; i<=n; i++){
		int mina=1e9,pos=0;
		for (int l=1; l<=n; l++){
			if (judge[l]==0 && mina>s[l]){
				mina=s[l];
				pos=l;
			}
		}
		if (pos==0) break;
		judge[pos]=1;
		for (int l=1; l<=n; l++){
			if (mp[pos][l]!=1e9 && s[l]>s[pos]+mp[pos][l])
				s[l]=s[pos]+mp[pos][l];
		}
	}
	cout<<s[n];
	return 0;
}
</pre>
</div>

  

posted @ 2017-09-11 16:41  |斗蜂|  阅读(123)  评论(0编辑  收藏  举报