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)
题意梗概:据说母牛在产奶的时候,因为奶量太充足,希望有人帮它挤奶,它回家就很快.我们便能喝到鲜美的 牛奶,不过,贫奶季节却大不相同,它会懒洋洋的在大草原上晃来晃去的晒太阳,而不会想到马上回家,这可不 ...
随机推荐
- D3.js(v3)+react框架 基础部分之数据绑定及其工作过程与绑定顺序
数据绑定: 将数据绑定到Dom上,是D3最大的特色.d3.select和d3.selectAll返回的元素的选择集.选择集上是没有数据的. 数据绑定就是使被选择元素里“含有”数据. 相关函数有两个: ...
- odoo开发笔记 -- 前台不同视图访问同一个模型
看一下partner这个表, 客户和供应商,都用这个表,那怎么区分呢: 供应商: 客户 注意这两个里面用domain来进行区分: <field name="domain" ...
- 原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想)
原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想) 总体思想: 希望让调用方通过 http调用传入一个需要生成图片的网页链接生成一个网页的图片并返回图片链接 ...
- (转)python WSGI框架详解
原文:https://www.cnblogs.com/shijingjing07/p/6407723.html?utm_source=itdadao&utm_medium=referral h ...
- 分享一套简单的CodeSmith三层模板
如果要连接mysql,需要安装驱动: https://cdn.mysql.com//Downloads/Connector-Net/mysql-connector-net-8.0.12.msi 连接字 ...
- php插入数据含有特殊符号的处理方法
我们在向mysql写入数据时,比如: mysql_query(”update table set `title`=’kuhanzhu’s blog’”); http://www.cnblogs.com ...
- scala-02-数组的操作
scala中的数组和 java中的数组一样, 定义了长度后不可改变 1, 产生一个数组: 有3种创建数组的方式, 分别直接new, 直接赋值, 或者使用 Array中的rang来产生 /** * 获取 ...
- 面试:vector类的简单实现
vector类的简单实现 #include <vector> #include <iostream> #include <cstring> #include < ...
- 【PyTorch深度学习60分钟快速入门 】Part0:系列介绍
说明:本系列教程翻译自PyTorch官方教程<Deep Learning with PyTorch: A 60 Minute Blitz>,基于PyTorch 0.3.0.post4 ...
- postcss
一.简介 PostCSS 本身是一个功能比较单一的工具.它提供了一种方式用 JavaScript 代码来处理 CSS.它负责把 CSS 代码解析成抽象语法树结构(Abstract Syntax Tre ...