POJ-2387 Til the Cows Come Home ( 最短路 )
题目链接: http://poj.org/problem?id=2387
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 最短路模板题,求1到n的最短路
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<queue> using namespace std; int way[][];
bool flag[];
int main(){
ios::sync_with_stdio( false ); int n, m; while( cin >> m >> n ){
int x, y, d;
memset( way, 0x3f3f3f3f, sizeof( way ) );
memset( flag, false, sizeof( flag ) );
for( int i = ; i < m; i++ ){
cin >> x >> y >> d;
way[x][y] = way[y][x] = min( way[x][y], d );
} for( int k = ; k < n - ; k++ ){
int minv = 0x3f3f3f3f, mini; for( int i = ; i < n; i++ ){
if( !flag[i] && minv > way[n][i] ){
minv = way[n][i];
mini = i;
}
} flag[mini] = true;
for( int i = ; i < n; i++ ){
if( !flag[i] ){
way[n][i] = min( way[n][i], way[n][mini] + way[mini][i] );
}
}
} cout << way[n][] << endl;
} return ;
}
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 ...
随机推荐
- Asp.NetCore源码学习[2-1]:配置[Configuration]
Asp.NetCore源码学习[2-1]:配置[Configuration] 在Asp. NetCore中,配置系统支持不同的配置源(文件.环境变量等),虽然有多种的配置源,但是最终提供给系统使用的只 ...
- Java高级面试题解析(二):百度Java面试题前200页(精选)
基本概念 操作系统中 heap 和 stack 的区别 heap是堆,stack是栈,是两种不同的数据结构.堆是队列优先,先进先出:栈是先进后出. 在java多线程中,每个线程都有自己的栈:不同的线程 ...
- JAVA基础知识(四):final关键字
final关键字可以用于成员变量.本地变量.方法以及类. 2. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误. 3. 你不能够对final变量再次赋值. 4. 本地 ...
- hadoop hdfs 分布式存储
1.克隆前的工作 1.配置好网络nat 需要设置静态ip并能通过主机上网 ssh 和 rsync 是必须下载的 2.yum install vim wget rsync ssh 并配 ...
- java之面向对象详解
#############java面向对象详解#############1.面向对象基本概念2.类与对象3.类和对象的定义格式4.对象与内存分析5.封装性6.构造方法7.this关键字8.值传递与引用 ...
- windows--OSError: [Errno 22] Invalid argument: '\u202aE:/desk/Desktop/test.txt' 读取文件的坑
准备打开文件时,报了如下错误: 在路径中出现了这个Unicode 202a字符,导致了这个错误. 这玩意是哪里来的? 复制windows文件属性的时候复制下图中的路径而来的. 解释: 这个字符的含义是 ...
- [HAOI2015]树上染色(树上dp)
[HAOI2015]树上染色 这种要算点对之间路径的长度和的题,难以统计每个点的贡献.这个时候一般考虑算每一条边贡献了哪些点对. 知道这个套路以后,那么这题就很好做了. 状态:设\(dp[u][i]\ ...
- xss实体绕过示例
知识点: 倘若是在script.input标签当中,即可突破. Payload ' oninput=alert`1` // 当要在input中输入内容时触发事件 ' oninput=alert`1` ...
- idea设置docker远程插件
简介 docker都是通过命令来操作容器,使用idea插件可以减少重复命令输入等. 使用步骤 Idea内安装插件 打开Idea,Preferences | Plugins 进入插件安装界面,在搜索框中 ...
- HuffmanTree
/* 例如,对于数列{pi}={5, 3, 8, 2, 9},Huffman树的构造过程如下: 1. 找到{5, 3, 8, 2, 9}中最小的两个数,分别是2和3,从{pi}中删除它们并将和5加入, ...