Til the Cows Come Home(最短路模板题)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
//题目意思是,第一行有两个整数n,m,说明有n个边,m个点,接下来n行,每行有三个整数,a,b,c,说明从 a 到 b 距离是多少,输出从1- n 的最小路程
//显然,这是一道水题,dijstra算法 4116kb 110ms
//dijkstra 算法
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MX 1005 int n,m;
int mp[MX][MX]; int dis[MX];
bool vis[MX]; void dijkstra()
{
memset(vis,,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
dis[]=; for(int i=;i<=n;i++)
{
int mim=INF,v;
for(int j=;j<=n;j++)
if(!vis[j] && dis[j]<mim)
{
v=j;
mim=dis[j];
}
vis[v]=;
for(int j=;j<=n;j++)
if(!vis[j] && dis[j]>mp[v][j]+dis[v])
dis[j]=mp[v][j]+dis[v];
}
printf("%d\n",dis[n]);
} int main()
{
while(~scanf("%d%d",&m,&n))
{
memset(mp,0x3f,sizeof(mp));
for(int i=;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(mp[a][b]>c) mp[a][b]=mp[b][a]=c;//只记最小的
}
dijkstra();
}
return ;
}
//spfa 算法,很牛逼 260kb 0ms过了
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MXN 1005
#define MXM 4010 struct Edge{
int to;
int w;
int nex;
}edge[MXM]; int n,m,r_m;
int headlist[MXN];
int dis[MXN];
int vis[MXN]; void spfa()
{
queue <int> Q;
memset(vis,,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
dis[]=;
vis[]=;
Q.push();
while(!Q.empty())
{
int u =Q.front();Q.pop();
vis[u]=;
for(int i=headlist[u];i!=-;i=edge[i].nex)
{
int to=edge[i].to, w=edge[i].w;
if(dis[u]+w<dis[to])
{
dis[to]=dis[u]+w;
if(!vis[to])
{
vis[to]=;
Q.push(to);
}
}
}
}
printf("%d\n",dis[n]);
} int main(){
int i,a,b,c;
while(~scanf("%d%d",&m,&n))
{
for(i=;i<=n;i++) headlist[i]=-;
r_m=;
for(i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
edge[r_m]=(Edge){b,c,headlist[a]};
headlist[a]=r_m;
edge[r_m+]=(Edge){a,c,headlist[b]};
headlist[b]=r_m+;
r_m+=;
}
spfa();
}
return ;
}
Til the Cows Come Home(最短路模板题)的更多相关文章
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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模板题)
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- 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(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- 牛客小白月赛6 I 公交线路 最短路 模板题
链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...
随机推荐
- DB2 With语句递归
WITH T1 (T11 , T22 , T33 , T44) AS (SELECT TASKID , REPLY , ROWNUMBER () OVER (PARTITION BY TASKID) ...
- 4C 2018 倒数的字符序列
输入L,N 将长度为L的小写字符串序列,比如L=3,序列为aaa,aab,aac,...aba,...zzz,求倒数第N个字符串序列是什么.输入3 7421 得到pat 第一感觉是A,B,C...,Z ...
- Codeforces 746G(构造)
G. ...
- Swift 基础部分(建议掌握OC字符串知识的翻阅)
更新说明: Swift 目前已经发布到4.0版本了,以前写的这整个Swift学习系列的文章,有很多的不足之处,我会重新整理整个系列文章,也是相当于重新复习一遍Swift,后面系列文章的改动之处全都会做 ...
- HTTP 状态消息 [转]
转自:https://www.cnblogs.com/wuyongyu/p/5745875.html HTTP 状态消息 ...
- 获取元素位置信息和所占空间大小(via:js&jquery)
工作中有一个很常见的需求,hover或者click某元素后,在该元素旁边出现弹框,主要就是获取该元素的位置坐标以及元素所占区块的大小.最近工作中就遇到了,发现js和jquery的实现方法有很大的区别, ...
- 关于 Android 平台开发相关的有哪些推荐书籍?
转自:http://www.zhihu.com/question/19579609 作者:Shan Huang 链接:http://www.zhihu.com/question/19579609/an ...
- Python标准库:1. 介绍
标准库包括了几种不同类型的库. 首先是那些核心语言的数据类型库,比方数字和列表相关的库.在核心语言手冊里仅仅是描写叙述数字和列表的编写方式,以及它的排列,而未定义它的语义. 换一句话说,核心语言手冊仅 ...
- JAVA学习第十四课(接口:implements及其基本应用)
接口: 我们知道抽象类中能够定义抽象方法,也能够定义非抽象方法.当一个抽象类中的方法都是抽象方法的时候,我们就能够定义还有一种表现方式:接口(interface),所以接口是一种特殊的抽象类 接口的出 ...
- -ROOT-表和.META.表结构详解
在<HBase技术简介>中我们知道,HBase中有两个特殊的表:-ROOT-和.META.. 由于HBase中的表可能非常大,故HBase会将表按行分成多个region,然后分配到多台Re ...