poj 2387 Til the Cows Come Home(kuangbin带你飞 专题四:最短路)
2015-02-01 16:02
337 查看
十分简单的模板题?幸亏我发现我博客里面有dijkstra的代码....
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<iostream> using namespace std; #define inf 0x7fffffff int map[1111][1111]; int n,m,dis[1111],used[1111]; int dijkstra() { for(int i=1;i<=n;i++) dis[i]=inf; memset(used,0,sizeof(used)); dis[1]=0; for(int i=1;i<n;i++) { int mini=inf,t=-1; for(int j=1;j<=n;j++) { if(dis[j]<mini&&!used[j]) { t=j; mini=dis[j]; } } used[t]=1; if(mini==inf) break; for(int j=1;j<=n;j++) { if(!used[j]&&dis[j]>mini+map[t][j]) dis[j]=mini+map[t][j]; } } return dis ; } int main() { while(cin>>m>>n) { int u,v,s; memset(map,127,sizeof(map)); for(int i=0;i<m;i++) { cin>>u>>v>>s; if(s<map[u][v]) map[u][v]=map[v][u]=s; } cout<<dijkstra()<<endl; } return 0; }
相关文章推荐
- [kuangbin带你飞]专题4 最短路 A - Til the Cows Come Home POJ - 2387
- POJ 2387 || Til the Cows Come Home(最短路,dijkstra
- POJ 2387 Til the Cows Come Home --最短路模板题
- POJ 2387 Til the Cows Come Home 最短路
- poj——2387——Til the Cows Come Home(简单最短路)
- 7_4_N题 Til the Cows Come Home 题解[poj 2387] (最短路)
- poj 2387 Til the Cows Come Home (最短路,dijkstra模版题)
- POJ 2387 Til the Cows Come Home(最短路dijkstra)
- POJ 2387 - Til the Cows Come Home(单源最短路)
- 【POJ 2387】Til the Cows Come Home(Dij最短路)
- 最短路dij——POJ 2387 Til the Cows Come Home题解
- POJ 2387 Til the Cows Come Home 【最短路(dijkstra)】
- POJ 2387 Til the Cows Come Home 最短路
- POJ 2387 Til the Cows Come Home BFS最短路求解
- POJ 2387 Til the Cows Come Home(最短路)
- poj 2387 Til the Cows Come Home -- 最短路dijstra
- 第一次最短路:POJ 2387 Til the Cows Come Home(bfs方法~)
- poj 2387 Til the Cows Come Home(最短路)
- POJ 2387 Til the Cows Come Home (单源最短路SPFA解法)
- POJ - 2387 Til the Cows Come Home (最短路)