Til the Cows Come Home

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 54716   Accepted: 18560

Description

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.

Source

 
dijkstra,坑点:有重边,取边权最小
 //2017-07-18
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
int t, n, G[N][N], dis[N], vis[N]; void dijkstra(int s, int d)
{
for(int i = ; i <= n; i++)
dis[i] = G[s][i];
dis[s] = ;
vis[s] = ;
int mindis, u;
for(int i = ; i <= n; i++)
{
mindis = inf;
for(int j = ; j <= n; j++)
if(!vis[j] && dis[j] < mindis)
{
mindis = dis[j];
u = j;
}
vis[u] = ;
for(int v = ; v <= n; v++)
{
if(dis[v] > dis[u]+G[u][v]){
dis[v] = dis[u]+G[u][v];
}
}
}
} int main()
{
int s, d, u, v, w;
while(cin>>t>>n)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
G[i][j] = inf;
dis[i] = inf;
vis[i] = ;
}
for(int i = ; i < t; i++)
{
cin>>u>>v>>w;
if(G[u][v] > w)
G[u][v] = G[v][u] = w;
}
dijkstra(n, );
cout<<dis[]<<endl;
} return ;
}
 import java.util.*; // 2018-03-28

 public class Main {
static final int INF = 0x3f3f3f3f;
static Graph graph;
static int [] dist; static boolean spfa(int s, int n) {
boolean [] vis = new boolean[n+1];
int [] cnt = new int[n+1];
for(int i = 1; i <= n; i++) {
dist[i] = INF;
vis[i] = false;
}
Queue<Integer> que = new LinkedList<Integer>();
que.offer(s);
dist[s] = 0;
vis[s] = true;
while(!que.isEmpty()) {
int u = que.poll();
vis[u] = false;
for(int i = graph.head[u]; i != -1; i = graph.edges[i].next) {
int v = graph.edges[i].v;
int w = graph.edges[i].w;
if(dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
if(!vis[v]) {
vis[v] = true;
que.offer(v);
if(++cnt[v] > n)return false;
}
}
}
}
return true;
} public static void main(String[] args) {
Scanner cin = new Scanner(System.in); int n, m;
while(cin.hasNext()) {
m = cin.nextInt();
n = cin.nextInt();
graph = new Graph(n, 2*m);
int u, v, w;
for(int i = 0; i < m; i++) {
u = cin.nextInt();
v = cin.nextInt();
w = cin.nextInt();
graph.addEdge(u, v, w);
graph.addEdge(v, u, w);
}
dist = new int[n+1];
if(spfa(1, n)) {
System.out.println(dist[n]);
}
}
}
} class Graph{
static class Edge{
int v, w, next; Edge(int _v, int _w, int _next){
this.v = _v;
this.w = _w;
this.next = _next;
}
} int n, m, tot;
int [] head;
Edge [] edges; Graph(int _n, int _m){
this.n = _n;
this.m = _m;
tot = 0;
head = new int[n+1];
edges = new Edge[m+1];
for(int i = 0; i <= n; i++)
head[i] = -1;
} void addEdge(int u, int v, int w) {
edges[tot] = new Edge(v, w, head[u]);
head[u] = tot++;
}
}

POJ2387(KB4-A)的更多相关文章

  1. poj2387 Til the Cows Come Home(Dijkstra)

    题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...

  2. (模板)poj2387(dijkstra+优先队列优化模板题)

    题目链接:https://vjudge.net/problem/POJ-2387 题意:给n个点(<=1000),m条边(<=2000),求结点n到结点1的最短路. 思路:dijkstra ...

  3. 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 ...

  4. POJ-2387(原始dijkstra求最短路)

    Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...

  5. poj2387 spfa求最短路

    //Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...

  6. 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 ...

  7. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  8. poj2387 初涉最短路

    前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码 ...

  9. poj2387 Til the Cows Come Home

    解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...

  10. 暑假集训(3)第三弹 -----Til the Cows Come Home(Poj2387)

    题意梗概:据说母牛在产奶的时候,因为奶量太充足,希望有人帮它挤奶,它回家就很快.我们便能喝到鲜美的 牛奶,不过,贫奶季节却大不相同,它会懒洋洋的在大草原上晃来晃去的晒太阳,而不会想到马上回家,这可不 ...

随机推荐

  1. 牛客第二场 C.message(计算几何+二分)

    题目传送:https://www.nowcoder.com/acm/contest/140/C 题意:有n个云层,每个云层可以表示为y=ax+b.每个飞机的航线可以表示为时间x时,坐标为(x,cx+d ...

  2. Zookeeper客户端Curator使用详解

    Zookeeper客户端Curator使用详解 前提 最近刚好用到了zookeeper,做了一个基于SpringBoot.Curator.Bootstrap写了一个可视化的Web应用: zookeep ...

  3. Dubbo原理实现之与spring融合

    Spring中bean的定义可以通过编程,可以定义在properties文件,也可以定义在通过xml文件中,用的最多的是通过xml形式,由于xml格式具有很好的自说明便于编写及维护.对于xml的文档结 ...

  4. 【TensorFlow】:解决TensorFlow的ImportError: DLL load failed: 动态链接库(DLL)初始化例程失败

    [背景] 在scikit-learn基础上系统结合数学和编程的角度学习了机器学习后(我的github:https://github.com/wwcom614/machine-learning),意犹未 ...

  5. 移动端测试接口--Fiddler抓包工具

    Fiddler抓包工具是一款免费且功能强大的数据包抓取软件.它通过代理的方式获取程序http通讯的数据,可以用其检测网页和服务器的交互情况,能够记录所有客户端和服务器间的http请求,支持监视.设置断 ...

  6. 架构模式数据源模式之:数据映射器(Data Mapper)

    一:数据映射器 关系型数据库用来存储数据和关系,对象则可以处理业务逻辑,所以,要把数据本身和业务逻辑糅杂到一个对象中,我们要么使用 活动记录,要么把两者分开,通过数据映射器把两者关联起来. 数据映射器 ...

  7. 十分钟内在Ubuntu系统上搭建Mono开发环境(Mono软件Ubuntu系统国内镜像源、Mono国内镜像源)

    Mono软件Ubuntu系统国内镜像源.Mono国内镜像源 http://download.githall.cn/repo 替换为国内源(非官方)有利于加快mono的安装速度,一般情况下,完成mono ...

  8. PHP:使用Zend对源码加密、Zend Guard安装以及Zend Guard Run-time support missing的解决方法

    Zend Guard是目前市面上最成熟的PHP源码加密产品了.刚好需要对自己的产品进行加密,折腾了一晚上,终于搞定,将碰到的问题及解决方法记录下来,方便日后需要,也可以帮助其他人.我使用的是Wamps ...

  9. tomcat 的自问自答与总结

    目录 1 tomcat 的加载问题,启动后更新是否自动加载 2 tomcat 的context.xml 文件读取顺序与覆盖原则 3 就是 建议的tomcat 配置 4 避免二次部署加载的问题 在查看了 ...

  10. mysql通信协议的半双工机制理解

    一.通信知识中的半双工概念 通信的方式分为:单工通信,半双工,全双工. 全双工的典型例子是:打电话.电话在接到声音的同时也会传递声音.在一个时刻,线路上允许两个方向上的数据传输.网卡也是双工模式.在接 ...