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)
题意梗概:据说母牛在产奶的时候,因为奶量太充足,希望有人帮它挤奶,它回家就很快.我们便能喝到鲜美的 牛奶,不过,贫奶季节却大不相同,它会懒洋洋的在大草原上晃来晃去的晒太阳,而不会想到马上回家,这可不 ...
随机推荐
- C语言Socket-单工通信(客户端向服务器发送数据)
服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...
- Mac 下查看端口是否被占用
1. lsof -i :8080 2. netstat -anp tcp | grep 8080 3. nc -w 10 -n -z 127.0.0.1 8070-8090
- 6_文件IO
1. 基本文件读取 readline(),readlines(),write(),writelines() f.read(size),指定读取文件的字节数,需要注意的是 ...
- odoo开发笔记 -- 数据库备份策略
odoo默认的数据库为postgresql数据库, PG是个非常强大的数据库,也是未来的一个趋势. 对于odoo的数据备份,odoo提供了自己的备份方式, 1. 从前台页面.输入odoo应用访问地址, ...
- DDD漫想
领域专用语言 领域驱动设计(Domain Driver Design)开发中,最令我震撼的是领域专用语言(Domain specific language),领域专用语言专注于描述当前领域内的业务细节 ...
- 02-03:springboot 整合listener
1.通过注解扫描完成Listener组件的注册 1.1 编写listener /** * Springboot 整合listener */ import javax.servlet.ServletCo ...
- JLS中表达式的所有文法
3.8. Identifiers Identifier: IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral Iden ...
- 图片按日期分类和查看程序(WPF开发)(附源码)
手机方便了我们的生活,可以随时随地拍摄.越来越多的图片堆砌在电脑里.看到杂乱无章的图片,实在感到头痛.手动整理太复杂.基于此,我写了一个小程序,可以将图片按日期整理和查看.按日期查看图片,回忆过去的点 ...
- java-数组连接的几种方式
多个数组进行拼接, 1, 使用java自己的 System#arrayCopy() byte[] message = new byte[heads.length + result.length + b ...
- 深入理解java中HelloWorld的执行流程
HelloWorld.java是我们学习java的第一个程序,简单的再也不能简单了,可是里面的原理以及执行流程大家都知道吗?最近在复习java知识,特地钻研了一番分享给大家! 贴出HelloWorld ...