POJ2387(KB4-A)
Til the Cows Come Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 54716 | Accepted: 18560 |
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
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
Source
//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)的更多相关文章
- poj2387 Til the Cows Come Home(Dijkstra)
题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...
- (模板)poj2387(dijkstra+优先队列优化模板题)
题目链接:https://vjudge.net/problem/POJ-2387 题意:给n个点(<=1000),m条边(<=2000),求结点n到结点1的最短路. 思路:dijkstra ...
- 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(原始dijkstra求最短路)
Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...
- poj2387 spfa求最短路
//Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...
- 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 初涉最短路
前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码 ...
- poj2387 Til the Cows Come Home
解题思路:最短路的模板题,注意一个细节处理即可. 见代码: #include<cstdio> #include<cstring> #include<algorithm&g ...
- 暑假集训(3)第三弹 -----Til the Cows Come Home(Poj2387)
题意梗概:据说母牛在产奶的时候,因为奶量太充足,希望有人帮它挤奶,它回家就很快.我们便能喝到鲜美的 牛奶,不过,贫奶季节却大不相同,它会懒洋洋的在大草原上晃来晃去的晒太阳,而不会想到马上回家,这可不 ...
随机推荐
- [Swift]lower_bound和upper_bound
时间复杂度:一次查询O(log n),n为数组长度. [C++] lower_bound:返回大于等于val的第一个值 功能:查找非递减序列[first,last) 内第一个大于或等于某个元素的位置. ...
- 小记 ArchLinux 下 Typora 无法输入中文
今天准备写一篇 Linux 下的打印机文章,打开 Typora 时我发现不管我怎么设置都无法输入中文. pacman -R typora pacman -S typora 重新安装是无效的,我突然想起 ...
- Linux - 针对用户账号的常用操作
用户目录 除root用户外,其他默认的用户目录一般为/home/<user name>. 可以通过如下步骤修改默认用户目录 修改/etc/passwd文件中相应用户的路径信息 停止此用户的 ...
- SQL 复习笔记 MSSQL篇
苦逼得很,一下就失业了,只有好好复习,迎接下一份工作 MSSQL篇: 1.数据库表分为临时表和永久表.临时表又分为全局临时表和局部临时表 全局临时表:表名以##开头.对系统当前 ...
- 【sping揭秘】12、SpringAOP的实现机制
SpringAOP的实现机制 设计模式代理模式 参考我之前的代理模式 http://www.cnblogs.com/cutter-point/p/5226642.html 这里写个简单的案例 pack ...
- POJ 2590
#include<iostream> #include<algorithm> #define MAXN 1000000 using namespace std; unsigne ...
- 09-01 Java final,多态,抽象类,接口
final /* final可以修饰类,方法,变量 特点: final可以修饰类,该类不能被继承. final可以修饰方法,该方法不能被重写.(覆盖,复写) final可以修饰变量,该变量不能被重新赋 ...
- 在matlab中实现线性回归和logistic回归
本文主要讲解在matlab中实现Linear Regression和Logistic Regression的代码,并不涉及公式推导.具体的计算公式和推导,相关的机器学习文章和视频一大堆,推荐看Andr ...
- hexo 静态页面生成后页面打不开的问题
我这里的原因是4000端口被占用了 *** hexo入门指南教程: 官方文档 用Hexo 3 搭建github blog 做一款hexo主题(进阶) 坑 1 要安装node和git 2 别忘了安装he ...
- Android:异步处理之AsyncTask的应用(二)
前言 在上一篇文章中<Android:异步处理之Handler+Thread的应用(一)>,我们知道Android的UI主线程主要负责处理用户的按键事件.用户的触屏事件以及屏幕绘图事件等: ...