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 ...
- [学习笔记]树套树 线段树套Splay
今天调了一个早上哈哈哈,不过因为\(Splay\),常数比较大 洛谷的评测记录: \(Code\ Below:\) #include <bits/stdc++.h> #define ll ...
- Linux学习笔记-基本操作3
1. vim编辑器的使用2. gcc编译器3. 静态库的制作 -- lib4. 动态库的制作 -- dll vi -- vimvim是从vi发展过来的一款文本编辑器vi a.txt前提: 安装了 ...
- mongodb的Snapshot 隔离级别(记住)
Snapshot 隔离和 Row Version的工作模式 当启用Snapshot隔离级别时,每一个更新数据的操作都会在tempdb中存储该行的原始副本,术语叫作行版本(RowVersion),SQL ...
- github 最新项目快报
http://www.open-open.com/github/view/github2016-10-17.html
- matlab中元胞数组的创建与内容读取
一.创建元胞数组 1.用cell命令创建规格为2*2的空元胞 >> a=cell(2,2) a = [] [] [] [] 2.用大括号"{}"创建元胞数组并赋值 &g ...
- python中@staticmethod与@classmethod
@ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...
- 业余实现一个统计A股数据工具
自己瞎捣鼓了几天 python,数据来源新浪财经,每天收盘启动爬虫抓取一遍,web 端呈现日线与周线数据:实时图表显示上证指数与个股指数等.技术点:scrapy apscheduler sqlalch ...
- Vue图片懒加载插件 - vue lazyload的简单使用
Vue module for lazyloading images in your applications. Some of goals of this project worth noting i ...
- Git学习系列之CentOS上安装Git详细步骤(图文详解)
前言 最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和Win ...