Til the Cows Come Home(spfa做法)
题目
题目描述
贝茜在谷仓外的农场上,她想回到谷仓,在第二天早晨农夫约翰叫她起来挤奶之前尽可能多地睡上一觉.由于需要睡个好觉,贝茜必须尽快回到谷仓.农夫约翰的农场上有N(2≤N≤1000)个路标,每一个路标都有唯一的编号(1到N).路标1是谷仓,路标N是贝茜一整天呆在那里的果树园.农场的所有路标之间共有T(1≤T≤2000)条不同长度的供奶牛走的小路(无方向).贝茜对她识别方向的能力不是很自信,所以她每次总是从一条小路的头走到尾,再以这条路的尾作为下一条路的头开始走. 现给出所有路标之间的小路,要求输出贝茜回到谷仓的最短路程(每组输入数据都保证有解).
输入
第1行:2个整数T和N.
第2到T+1行:每行用空格隔开的三个整数描述一条小路.前两个整数是这条小路的尾和头,
第三个整数是这条小路的长度(不大于100).
输出
一个整数,表示贝茜从路标N到路标1所经过的最短路程
样例输入
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
样例输出
90
分析
spfa裸题。
链式前向星存图,spfa跑一遍完事。
上代码。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N = ;
const int M = ;
int n, m;
int head[N], nex[M], to[M], val[M], ce;
void add(int u, int v, int w) {
to[++ce] = u; nex[ce] = head[v]; head[v] = ce; val[ce] = w;
to[++ce] = v; nex[ce] = head[u]; head[u] = ce; val[ce] = w;
}
int d[N];
bool used[N];
queue<int> q;
void spfa(int s) {
memset(d, 0x3f, sizeof d);
d[s] = ;
q.push(s);
used[s] = true;
while(!q.empty()) {
int u = q.front(); q.pop();
used[u] = false;
for(int i=head[u]; i; i=nex[i]) {
int v = to[i], w = val[i];
if(d[u] + w < d[v]) {
d[v] = d[u] + w;
if(!used[v]) {
used[v] = true;
q.push(v);
}
}
}
}
}
int main() {
scanf("%d%d", &m, &n);
for(int i=; i<=m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
spfa();
printf("%d\n", d[n]);
return ;
}
Til the Cows Come Home(spfa做法)的更多相关文章
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- Til the Cows Come Home(最短路)
Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
随机推荐
- Safari的CSS HACK方法
以前的文章里提到过IE6/IE7/IE8/IE9/.Firefox的CSS HACK方法,那么Safari的CSS HACK是什么呢? 请看以下CSS代码: .box { color: black ...
- 牛客小白月赛18 G Forsaken的三维数点
思路: 这是一道树状数组和二分的题,用线段树空间直接爆,时间也会超 然后这道题我犯了一个很低级的错误,导致我wa了十发左右,一个int型变量用lld输入,然后他给的提示是运行错误,我哭了,我一直以为是 ...
- Optional int parameter 'pId' is present but cannot be translated into a null value due to being declared as a primitive type.
接口测试的时候遇到了一个问题,导致测试阻断了好久,在此记录,谨防忘记. 具体报错如下: Optional int parameter 'pId' is present but cannot be tr ...
- svnadmin - Subversion 仓库管理工具
SYNOPSIS 总览 svnadmin command /path/to/repos [options] [args] OVERVIEW 概述 Subversion 是一个版本控制系统,允许保存旧版 ...
- Dubbox服务的消费方配置
在src/main/resources下创建applicationContext-web.xml <?xml version="1.0" encoding="UTF ...
- Vuex白话教程第六讲:Vuex的管理员Module(实战篇)
写在前面 这一讲是 Vuex 基础篇的最后一讲,也是最为复杂的一讲.如果按照官方来的话,对于新手可能有点难以接受,所以想了下,决定干脆多花点时间,用一个简单的例子来讲解,顺便也复习一下之前的知识点. ...
- 详解 MySQL int 类型的长度值问题
以下是每个整数类型的存储和范围 (来自 mysql 手册)
- shell编写启动脚本
[root@confluence bin]# vim /etc/init.d/confluence #!/bin/bash # Confluence Linux service controller ...
- 在linux设置/etc/vimrc 将vim 中后缀.sh的文件 的前几行进行默认输入
输入vim test.sh 新建后缀sh的文件,效果如下: 具体/etc/vimrc配置为: if expand("%:e") == 'sh' call setline(1,&q ...
- spring在注解标注的方法上加切面
之前以为只能在方法签名上加切面,今天发现注解上也能加切面 1.自定义一个注解(任意注解都可以,不一定是自定义的) @Target({ElementType.METHOD}) @Retention(Re ...