POJ - 2387 Til the Cows Come Home (最短路入门)
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
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
* Line 1: Two integers: T and N
* 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
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
很简单dijkstra就可以过,入门级的题目,这个题建议不要抄模板,直接自己敲,就是用来练习最短路的,模板始终不是自己的。
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1e3+;
const double esp=1e-;
//-------------------------------------------------------// int n,m; //n个节点,m条边
int dis[maxn][maxn];
bool vis[maxn];
int d[maxn];
inline void dijstra();
int main()
{
mem(dis,0x3f);
mem(vis,);
mem(d,0x3f);
for(int i=; i<=n; i++)dis[i][i]=;
cini(m),cini(n);//输入边数//点数
for(int i=; i<m; i++)
{
int x,y,z;
cini(x),cini(y),cini(z);
dis[x][y]=min(dis[x][y],z);
dis[y][x]=min(dis[y][x],z);
//cout<<dis[x][y]<<endl;
}
dijstra();
cout<<d[n]<<endl;
}
inline void dijstra()
{
d[]=;
for(int i=; i<=n; i++)
{
int x=;
for(int j=; j<=n; j++)
if(!vis[j]&&(d[j]<d[x]||x==))x=j; vis[x]=;
for(int j=;j<=n;j++)
d[j]=min(d[j],d[x]+dis[x][j]); }
}
POJ - 2387 Til the Cows Come Home (最短路入门)的更多相关文章
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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 ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
随机推荐
- docker go代码编译上传
一.找需要编译的项目 env GOOS=linux GOARCH=amd64 go build . //编译代码 二.编辑Dockerfile文件, Dcokerfile文件配置: https://w ...
- 2017蓝桥杯最大公共子串(C++B组)
题目: 最大公共子串长度问题就是:求两个串的所有子串中能够匹配上的最大长度是多少.比如:"abcdkkk" 和 "baabcdadabc",可以找到的最长的公共 ...
- C语言 文件复制
有很多人会问,学会C语言能干啥?,就只能控制台敲个数学题,做个界面都没有的贪吃蛇么? 刚开始的我,也是这样想的,但慢慢深入C语言后,我才领略到C的强大,C的万能.小到游戏破解,加解密算法,大到设备驱动 ...
- "强调内容"组件:<em> —— 快应用组件库H-UI
 <import name="em" src="../Common/ui/h-ui/text/c_tag_i"></import> & ...
- AtomicInteger的并发处理
AtomicInteger的并发处理 博客分类: Effective Java JDK1.5之后的java.util.concurrent.atomic包里,多了一批原子处理类.主要用于在高并发环 ...
- java的图形化界面 文本区JTextArea的程序例子
package java1; //使用时这个改成你的包名即可//文本区 JTextArea import javax.swing.*;import java.awt.*;import java ...
- php.ini中文详解
[PHP] ;;;;;;;;;;;;;;;;;;;;;;; ; 关于 php.ini 配置文件 ; ;;;;;;;;;;;;;;;;;;;;;;; ; PHP 的初始化文件, 必须命名为 php.in ...
- stand up meeting 11/27/2015-11/29/2015
part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 确定释义显示方案并进行代码实现: 4 完成UI设计的各项动能按钮的代码实现 6 数据库 朱玉影 导入了4 ...
- vue-element-admin执行npm install 报错
如果你出现这类报错: 那么恭喜你,因为这个问题很好解决. ----------------------- 解决方法: git config --global url."https://&qu ...
- Shellshock远程命令注入(CVE-2014-6271)漏洞复现
请勿用于非法用法,本帖仅为学习记录 shelshocke简介: shellshock即unix 系统下的bash shell的一个漏洞,Bash 4.3以及之前的版本在处理某些构造的环境变量时存在安全 ...