POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
#include<stdio.h>
#include<string.h>
#include<limits.h> int a[][];
int dis[],b[];
int n; int min(int x,int y)
{
return x<y?x:y;
} void dij(int k)
{
int i,j,mind,minj;
memset(b,,sizeof(b));
for(i=;i<=n;i++){
dis[i]=INT_MAX;
}
dis[k]=;
for(i=;i<n;i++){
mind=INT_MAX;
for(j=;j<=n;j++){
if(!b[j]&&dis[j]<mind){
mind=dis[j];
minj=j;
}
}
b[minj]=;
for(j=;j<=n;j++){
if(!b[j]&&a[minj][j]>=){
dis[j]=min(dis[j],dis[minj]+a[minj][j]);
}
}
}
} int main()
{
int t,x,y,z;
memset(a,-,sizeof(a));
scanf("%d%d",&t,&n);
while(t--){
scanf("%d%d%d",&x,&y,&z);
if(a[x][y]!=-){
if(z<a[x][y]){
a[x][y]=z;
a[y][x]=z;
}
}
else{
a[x][y]=z;
a[y][x]=z;
}
}
dij();
printf("%d\n",dis[n]);
return ;
}
优化Dij:
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<vector>
#include<queue>
using namespace std; struct Node{
int v,w;
friend bool operator<(Node a,Node b)
{
return a.w>b.w;
}
}node; vector<Node> a[];
int dis[];
int n; void dij(int k)
{
int v1,v2,i;
priority_queue<Node> q;
dis[k]=;
node.w=;
node.v=k;
q.push(node);
while(q.size()){
v1=q.top().v;
q.pop();
for(i=;i<a[v1].size();i++){
v2=a[v1][i].v;
if(dis[v2]>dis[v1]+a[v1][i].w){
dis[v2]=dis[v1]+a[v1][i].w;
node.w=dis[v2];
node.v=v2;
q.push(node);
}
}
}
} int main()
{
int t,x,y,z,i;
scanf("%d%d",&t,&n);
for(i=;i<=n;i++){
a[i].clear();
dis[i]=INT_MAX;
}
while(t--){
scanf("%d%d%d",&x,&y,&z);
node.w=z;
node.v=y;
a[x].push_back(node);
node.v=x;
a[y].push_back(node);
}
dij();
printf("%d\n",dis[n]);
return ;
}
POJ 2387 Til the Cows Come Home Dijkstra求最短路径的更多相关文章
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- POJ 2387 Til the Cows Come Home (Dijkstra)
传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
随机推荐
- ELK日志收集系统搭建
架构图 ELK 架构图:其中es 是集群,logstash 是单节点(猜想除非使用nginx对log4j的网络输出分发),kibana是单机(用不着做成集群). 1.拓扑图 2.logstash ...
- 安装pymysqlpool并使用(待补充)
pip3 install PyMysqlPool 第一个错,提示没有装c++ 14.0,下载安装报下一个错 error: Setup script exited with error: Microso ...
- HttpWebRequest的timeout和ReadWriteTimeout(转载)
公司[1]一牛人看我的代码,说我设置的timeout有误,还应该设置ReadWriteTimeout.本人很不服,于是上网查看了相关说明. HttpWebRequest httpWebRequest ...
- Django的基础操作总结
1:准备开始 建立一个新的project: django-admin.py startproject XXXXXX(名称) 建立一个新的App:python manage.py startapp XX ...
- poj1328 Radar Installation —— 贪心
题目链接:http://poj.org/problem?id=1328 题解:区间选点类的题目,求用最少的点以使得每个范围都有点存在.以每个点为圆心,r0为半径,作圆.在x轴上的弦即为雷达可放置的范围 ...
- make和rest用法
位置(position):下一个要读取或写入的数据的索引.缓冲区的位置不能为负,并且不能大于其限制(limit). 标记(mark)与重置(reset):标记是一个索引,通过Buffer中的mark( ...
- ubuntn14.04 使用 nvm创建多版本node环境
1. 下载 nvm wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash 2. 然后 ...
- JavaScript(3)
var a=90; switch(a){ case "890": window.alert("ok"); break; case 90: window.aler ...
- 三款功能强大代码比较工具Beyond compare、DiffMerge、WinMerge
我们经常会遇到需要比较同一文件的不同版本,特别是代码文件.如果人工去对比查看,势必费时实力还会出现纰漏和错误,因此我们需要借助一些代码比较的工具来自动完成这些工作.这里介绍3款比较流行且功能强大的工具 ...
- linkedhashSet和hashSet和TreeSet的区别(转)
Set接口Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false.Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就是说,只要两个对象用eq ...