POJ2387 Til the Cows Come Home -DIJKSTRA 练习
来源:互联网 发布:淘宝的电子商务系统 编辑:程序博客网 时间:2024/06/06 17:48
题目大意是:有N个牛棚和T条边相连,每条边有个权值,问1号到N号牛棚之间的最短距离
本题是又是DIJKSTRA最短路水题,注意任何两个牛棚之间可能有多条路相连,输入时先输入边,再输入点,程序如下:
#include<stdio.h>#include<stdlib.h>#include<string.h>const int maxn=1010,maxint=1000000000;int a[maxn][maxn],d[maxn],p[maxn];int main(){ int i,j,k,l,m,n; int x,y,z; scanf("%d%d",&m,&n); for(i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&z); if(!a[x][y]){ a[x][y]=z; a[y][x]=z; }else{ a[x][y]=a[x][y]<z?a[x][y]:z; a[y][x]=a[y][x]<z?a[y][x]:z; } } for(i=1;i<=n;i++)d[i]=maxint; d[1]=0; int mind; for(i=1;i<=n;i++){ mind=maxint; for(j=1;j<=n;j++) if(!p[j] && d[j]<mind){ mind=d[j]; k=j; } p[k]=1; for(j=1;j<=n;j++) if(!p[j] && a[k][j] && d[j]>d[k]+a[k][j]) d[j]=d[k]+a[k][j]; } printf("%d\n",d[n]); return 0; }
0 0
- POJ2387 Til the Cows Come Home -DIJKSTRA 练习
- POJ2387 Til the Cows Come Home【Dijkstra】
- POJ2387----Til the Cows Come Home(dijkstra)
- POJ2387 Til the Cows Come Home——Heap+dijkstra
- POJ2387 Til the Cows Come Home(dijkstra)
- POJ2387--Til the Cows Come Home--Dijkstra算法裸题
- poj2387-Til the Cows Come Home dijkstra入门水题
- poj2387 Til the Cows Come Home—Dijkstra模板
- POJ2387 Til the Cows Come Home(dijkstra求图论最短路)
- POJ2387 Til the Cows Come Home(dijkstra + heap 或 SPFA)
- POJ2387--Til the Cows Come Home(dijkstra算法)
- poj2387 Til the Cows Come Home (dijkstra)
- POJ2387 - Til the Cows Come Home(Dijkstra)
- poj2387~Til the Cows Come Home(最短路dijkstra)
- poj2387 Til the Cows Come Home(dijkstra)
- POJ2387--Til the Cows Come Home(dijkstra)
- poj2387- Til the Cows Come Home
- poj2387- Til the Cows Come Home
- 这两周的学校生涯回顾
- qq聊天布局思路
- UVA 10617 区间DP
- Android: SystemUI
- Memcache与python
- POJ2387 Til the Cows Come Home -DIJKSTRA 练习
- CentOS 7安装fcitx中文输入法
- UIImageView摘录(持续更新)
- Tease Apart Inheritance
- 关于HoTween移动
- 弱联网手游如何防作弊?
- 我的csdn之旅启程
- [MFC] CButtonST
- 初次下载STC15F204EA遇到的失败问题