POJ 2387 链式前向星下的SPFA
(POJ)[http://poj.org/problem?id=2387]
Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 69789 Accepted: 23386
Description
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
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
Line 1: Two integers: T and N
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.
OutputLine 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
Source
USACO 2004 November
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1e5;
const int maxm = 1050;
const int inf = 0x3f3f3f3f;
int n,m;
struct node
{
int to,w,next;
}e[maxn];
int k;
int head[maxn];
int dis[maxn],vis[maxn];
void add(int u,int v,int w)
{
e[k].to=v;
e[k].next=head[u];
e[k].w=w;
head[u]=k++;
}
queue<int> q;
void spfa(int st)
{
for(int i=1;i<=n;i++) dis[i]=inf;
dis[st]=0;
vis[st]=1;
q.push(st);
while(!q.empty())
{
int now = q.front();
q.pop();
vis[now]=0;
for(int i=head[now]; i; i=e[i].next)
{
int ed = e[i].to;
if(dis[ed] > dis[now] + e[i].w)
{
dis[ed] = dis[now]+e[i].w;
if(!vis[ed])
{
q.push(ed);
vis[ed]=1;
}
}
}
}
}
//79MS
int main()
{
int x,y,z;
while(cin>>m>>n)
{
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
k=1;
for(int i=1;i<=m;i++)
{
cin>>x>>y>>z;
add(x,y,z);
add(y,x,z);
}
spfa(1);
printf("%d\n",dis[n]);
}
}
/*
给了一个无向图,输入t和n,
t代表几个顶点,n代表问的是从第一个顶点到第n的顶点的最短距离,
各种最短路算法的模板题
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
*/
POJ 2387 链式前向星下的SPFA的更多相关文章
- POJ 1511 链式前向星+SPFA
#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; const i ...
- 【模板】链式前向星+spfa
洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...
- 链式前向星版DIjistra POJ 2387
链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)
题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...
- 链式前向星写法下的DFS和BFS
Input 5 7 1 2 2 3 3 4 1 3 4 1 1 5 4 5 output 1 5 3 4 2 #include<bits/stdc++.h> using namespace ...
- 链式前向星+SPFA
今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...
- 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...
随机推荐
- Java IO 小结
Java IO 的学习需要明白流设计的体系结构,这样才能在实际需要的时候,通过API文档查阅,快速实现功能.
- DataBase -- Employees Earning More Than Their Managers My Submissions Question
Question: The Employee table holds all employees including their managers. Every employee has an Id, ...
- 【题解】APIO2007动物园
首先一眼感受到这题特别的性质……5个?这么小的,感觉就像是状压.脑补了一下,如果没有环的话应该很好做吧……有环怎么办?5真的很小的,随便乱搞肯定也可以.那就放在外面暴力枚举吧.然后正解就出来了. 然而 ...
- [CF1041E]Tree Reconstruction
题目大意:有一棵树,现在给你每条树边被去掉时,形成的两个联通块中点的最大的编号分别是多少,问满足条件的树存不存在,存在输出方案 题解:一条边的两个编号中较大的一个一定是$n$,否则无解. 开始构造这棵 ...
- CCmdUI
原文链接地址:http://blog.csdn.net/luicha/article/details/6771185 CCmdUI是一个只被使用于ON_UPDATECOMMAND_UI消息的响应函数中 ...
- git使用笔记(五)打标签
By francis_hao Nov 19,2016 当一个项目commit了若干次到了一个可以发布版本的时候一般会给当前的分支状态打一个标签,就像我们常常见到的V1.0之类的. Git 使用的 ...
- HDU 3446 有贪心思想的01背包
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- PAT团体程序设计大赛---(模拟)
L1-1 古风排版(20 分) 中国的古人写文字,是从右向左竖向排版的.本题就请你编写程序,把一段文字按古风排版. 输入格式: 输入在第一行给出一个正整数N(<100),是每一列的字符数.第二行 ...
- 解决perm size out of memeory的问题
在idea中配置如下即可 -Xms1024m -Xmx1024m -XX:MaxNewSize=512m -XX:MaxPermSize=512m 如下图所示:
- Windows下安装Mycat-web
Mycat-web是基于Mycat的一个性能监控工具,如:sql性能监控等. 在安装Mycat-web之前需要先安装Zookeeper: 可参考: http://blog.csdn.net/tlk20 ...