POj2387——Til the Cows Come Home——————【最短路】
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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<algorithm>
using namespace std;
const int maxn=1200;
const int INF=1e9;
bool vis[maxn];
int d[maxn];
int w[maxn][maxn];
int T,N;
void Dijkstra(int st){ for(int i=0;i<N;i++){ int x,m=INF;
for(int y=N;y>=1;y--){ if(!vis[y]&&d[y]<=m)
m=d[x=y];
}
vis[x]=1;
for(int y=N;y>=1;y--){ d[y]=min(d[y],d[x]+w[x][y]);
}
}
printf("%d\n",d[1]);
}
void init(){ for(int i=0;i<=N;i++){ d[i]=(i==N?0:INF);
for(int j=0;j<=N;j++){ if(i==j)
w[i][j]=0;
else
w[i][j]=w[j][i]=INF;
}
}
memset(vis,0,sizeof(vis));
}
int main(){ while(scanf("%d%d",&T,&N)!=EOF){ init();
for(int i=1;i<=T;i++){ int u,v,dw;
scanf("%d%d%d",&u,&v,&dw);
w[u][v]=w[v][u]=min(dw,w[u][v]);//去重
}
Dijkstra(N);
}
return 0;
}
POj2387——Til the Cows Come Home——————【最短路】的更多相关文章
- POJ2387 Til the Cows Come Home (最短路 dijkstra)
AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...
- POJ-2387 Til the Cows Come Home ( 最短路 )
题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- poj2387 Til the Cows Come Home 最短路径dijkstra算法
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- poj2387 Til the Cows Come Home
解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...
- POJ2387 Til the Cows Come Home 【Dijkstra】
题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...
- POJ-2387.Til the Cows Come Home.(五种方法:Dijkstra + Dijkstra堆优化 + Bellman-Ford + SPFA + Floyd-Warshall)
昨天刚学习完最短路的算法,今天开始练题发现我是真的菜呀,居然能忘记邻接表是怎么写的,真的是菜的真实...... 为了弥补自己的菜,我决定这道题我就要用五种办法写出,并在Dijkstra算法堆优化中另外 ...
随机推荐
- 零成本实现WEB性能测试(二)JMeter基础知识
特点: 支持多种服务类型进行测试,包括: Web-Http,HTTPS SOAP Database via JDBC LDAP JMS Mail-POP3 & IMAP 支持录制回放方式获取脚 ...
- Replication--将LSN转换成16进制
在复制中经常会使用到16进制的LSN,但在日志fn_dblog中的LSN是数字形式,于是从网上找到以下转换函数CREATE FUNCTION dbo.fn_convertnumericlsntobin ...
- K8s集群安装--最新版 Kubernetes 1.14.1
K8s集群安装--最新版 Kubernetes 1.14.1 前言 网上有很多关于k8s安装的文章,但是我参照一些文章安装时碰到了不少坑.今天终于安装好了,故将一些关键点写下来与大家共享. 我安装是基 ...
- 编写高质量JS代码上
想写出高效的javascript类库却无从下手: 尝试阅读别人的类库,却理解得似懂给懂: 打算好好钻研js高级函数,但权威书上的内容太零散, 即使记住“用法”,但到要“用”的时候却没有想“法”. 也许 ...
- webservice简单例子
1.添加web服务. /// <summary> /// demo 的摘要说明 /// </summary> [WebService(Namespace = "htt ...
- 「SHOI2016」黑暗前的幻想乡
题目链接 戳我 \(Describe\) \(n−1\)个公司,每个公司能修一些边,求每条边都让不同的公司来修的生成树的方案数 \(Solution\) 这道题很明显容斥.答案就是:所有都选的生成树个 ...
- OCP最新题库收集,新版052考题及答案整理-19
19.Which is true about invalid PL/SQL objects? A) They are automatically recompiled against the new ...
- CVE-2012-2122-Mysql身份认证漏洞及利用
一.漏洞简介 当连接MariaDB/MySQL时,输入的密码会与期望的正确密码比较,由于不正确的处理,会导致即便是memcmp()返回一个非零值,也会使MySQL认为两个密码是相同的.按照公告说法大约 ...
- css中的左右垂直居中的问题,可兼容各种版本浏览器的写法
如题分为垂直居中,左右居中,先挑简单的记录. 一.左右居中 1.我刚开始写代码的时候,老师就直接告诉我一个简单的方法,那就是: width:500px; height:200px; margin:0 ...
- docker导入导出镜像
docker容器导入导出有两种方法: 一种是使用save和load命令 使用例子如下: docker save ubuntu:load>/root/ubuntu.tar docker load& ...