题目
题目描述
贝茜在谷仓外的农场上,她想回到谷仓,在第二天早晨农夫约翰叫她起来挤奶之前尽可能多地睡上一觉.由于需要睡个好觉,贝茜必须尽快回到谷仓.农夫约翰的农场上有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做法)的更多相关文章

  1. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  2. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  3. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  4. 怒学三算法 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 ...

  5. 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 ...

  6. Til the Cows Come Home(最短路)

    Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  7. 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 ...

  8. 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 ...

  9. Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)

    Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...

  10. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

随机推荐

  1. camunda用户的一些简单操作

    act_id_group:存放组信息act_id_membership:存放用户与组的相关信息act_id_user:存放用户act_id_info:存放用户个人信息act_id_tenant:存放租 ...

  2. QQ邮箱客户端配置

    接收协议:IMAP 接收邮箱服务器地址:imap.qq.com 端口:993 加密方法:TLS 发送协议:SMTP 发送服务器:smtp.qq.com 端口:465 加密方法:TLS

  3. FrameWork内核解析之PackageMS启动(一)下篇

    阿里P7Android高级架构进阶视频免费学习请点击:https://space.bilibili.com/474380680 1. PackageMS相关框架类   ​   2.1 PackageM ...

  4. Java技术专区-虚拟机系列-虚拟机参数(常用)

    基础参数系类(内存分配) -server:一定要作为第一个参数,在多个CPU时性能佳 -Xmn:young generation的heap大小,一般设置为Xmx的3.4分之一-Xms:初始Heap大小 ...

  5. 《代码大全2》读书笔记 Week9

    本周阅读了<代码大全2>第14章至第17章,这几章对我们熟悉的直线型代码.条件语句.循环语句和一些不常用的控制结构(如goto.try-catch结构)提出了一些使用建议,以下分享条件语句 ...

  6. 使用jquery Ajax异步刷新 下拉框

    一个下拉框 <label>产品类型:</label> <select id="protype" name="protype" on ...

  7. Elasticsearch添加Shield后TransportClient如何连接?

    Elasticsearch添加Shield后TransportClient如何连接? 时间 2015-12-28 10:24:01  旁门左道 原文  http://log.medcl.net/ite ...

  8. Codeforces 319C DP 斜率优化

    题意:在一颗森林有n颗数,编号从1到n,第i棵树高度是a[i].有一个伐木工想要砍伐这片森林,它的电锯每次可以将树的高度减少1,然后就必须要充电,充电的代价是他已经砍倒的树种编号最大的那颗树的代价(b ...

  9. 华视身份证读卡器获取uid号vue单文件组件版本

    <template> <div id="app" ref="app"> <object ref="CertCtl&quo ...

  10. CF734E Anton and Tree

    \(\mathtt{CF734E}\) \(\mathcal{Description}\) 给一棵\(n(n\leq200000)\)个节点的树,每个点为黑色或白色,一次操作可以使一个相同颜色的连通块 ...