POJ2387-Till the cows come home【最短路】
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.
用的spfa算法 复杂度O(ME)
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstring>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
int t, n;
int a[1005][1005], dis[1005];
bool vis[1005];
void spfa(int s)
{
for(int i = 1; i <= n; i++){
dis[i] = inf;
vis[i] = false;
}
dis[s] = 0;
vis[s] = true;
queue<int> que;
que.push(s);
int i, v;
while(!que.empty()){
v = que.front();que.pop();
vis[v] = false;
for(i = 1; i <= n; i++){
if(a[v][i] > 0 && dis[i] > dis[v] + a[v][i]){
dis[i] = dis[v] + a[v][i];
if(vis[i] == 0){
que.push(i);
vis[i] = true;
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&t,&n)){
memset(a, inf, sizeof(a));
for(int i = 0; i < t; i++){
int f, t, w;
cin>>f>>t>>w;
a[f][t] = min(a[f][t], w);
a[t][f] = min(a[t][f], w);
}
spfa(n);
cout<<dis[1]<<endl;
}
return 0;
}
要注意处理重边 注意先输入的是边的数目t
POJ2387-Till 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——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- POJ-2387Til the Cows Come Home,最短路坑题,dijkstra+队列优化
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K http://poj.org/problem?id=238 ...
- 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(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- poj2387 Til the Cows Come Home
解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...
- (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】
题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...
随机推荐
- SQLServer转MYSQL的方法(连数据)[传]
转自 https://blog.csdn.net/AlbenXie/article/details/77449720 SQLServer转MYSQL的方法(连数据) 本次转换需要依赖使用工具Navic ...
- ios利用Reachability确认网络环境3G/WIFI(转)
iPhone开发技巧之网络篇(4)--- 确认网络环境 开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息.如果没有处理它们,是不会通过Apple的审查的. Apple 的 例程 Re ...
- InnoDB锁问题 & DB事务隔离级别
<参考:http://www.cnblogs.com/jack204/archive/2012/06/09/2542940.html>InnoDB行锁实现方式InnoDB行锁是通过给索引上 ...
- IIS------项目配置到IIS后报500错误
转载: http://blog.csdn.net/yinjingjing198808/article/details/7185453 错误一: HTTP Error 500.19 - Internal ...
- phonegap入门–3 Android phonegap 自定义插件DEMO
一.环境要求: 首先需要建立phonegap android 工程,请参考:http://www.cnblogs.com/zhujinguo/p/4369883.html 二.建立java类 ...
- 8 -- 深入使用Spring -- 3...1 Resource实现类InputStreamResource、ByteArrayResource
8.3.1 Resource实现类------InputStreamResource:访问输入流资源的实现类.ByteArrayResource:访问字节数组资源的实现类. 5. 访问字节数组资源 ⊙ ...
- 使用webbench做压力测试
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.下文介绍的是在Ubu ...
- 采用get方式提交数据到服务器实例
GetDemo项目目录 一.编写StreamTools.java /** * */ package com.hyzhou.getdemo.utiils; import java.io.ByteArra ...
- Empire安装和试用
Empire2.0,powershell后渗透利器的安装和试用 https://www.restkhz.com/2017/05/empire-test/ 使用Empire“探索”Windows个人电脑 ...
- NFS 常见报错
问题:客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主属组都为nobody解决方法:这种情况会出现在 centos6 或 NFS 4版本中,只要在挂载的时候加上 -o nfsve ...