HNU 13375 Flowery Trails (spfa最短路)
求最短路径覆盖的全部边权值和。
思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径。
这题很好
- #include <cstring>
- #include <cmath>
- #include <queue>
- #include <vector>
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- const int maxn = ;
- const int maxm = ;
- const int INF = 0x3f3f3f3f;
- int n, m;
- struct ee {
- int to;
- int nxt;
- int w;
- } edge[maxm];
- int head[maxn], tol;
- void init() {
- memset(head, -, sizeof head );
- tol = ;
- }
- void add(int u, int v, int w) {
- edge[tol].to = v;
- edge[tol].w = w;
- edge[tol].nxt = head[u];
- head[u] = tol++;
- }
- int d1[maxn], d2[maxn];
- bool vis[maxn];
- void spfa(int s, int t, int d[]) {
- for(int i=; i<n; ++i) d[i] = INF;
- memset(vis, false, sizeof vis );
- queue<int> q;
- q.push(s);
- d[s] = ;
- vis[s] = true;
- while(!q.empty()) {
- int u = q.front();
- q.pop();
- vis[u] = false;
- for(int i=head[u]; ~i; i=edge[i].nxt) {
- int &v = edge[i].to;
- int &cost = edge[i].w;
- if(d[v] > d[u] + cost) {
- d[v] = d[u] + cost;
- if(!vis[v]) {
- vis[v] = true;
- q.push(v);
- }
- }
- }
- }
- }
- void solve() {
- int s = , t = n-;
- spfa(s, t, d1);
- spfa(t, s, d2);
- ll ans = ;
- int minn = d1[t];
- for(int u=; u<n; u++) {
- for(int i=head[u]; ~i; i=edge[i].nxt) {
- int &v=edge[i].to;
- int &cost=edge[i].w;
- if(d1[u]+d2[v]+cost==minn) {
- ans+=cost;
- }
- }
- }
- printf("%I64d\n", ans*);
- }
- int main() {
- int u, v, l;
- while(~scanf("%d%d", &n, &m)) {
- init();
- for(int i=; i<m; ++i) {
- scanf("%d%d%d", &u, &v, &l);
- add(u, v, l);
- add(v, u, l);
- }
- solve();
- }
- return ;
- }
HNU 13375 Flowery Trails (spfa最短路)的更多相关文章
- UVALive 6885 Flowery Trails 最短路
Flowery Trails 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...
- NOIP2013 华容道 (棋盘建图+spfa最短路)
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...
- UVALive 6885 Flowery Trails 最短路枚举
题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...
- poj1502 spfa最短路
//Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...
- HNU 12847 Dwarf Tower(最短路+队列优化)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847 解题报告:有n样物品,编号从1到n第i样物品可以通过金 ...
- hiho(1081),SPFA最短路,(非主流写法)
题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...
- hdu 5545 The Battle of Guandu spfa最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- BZOJ 1003 物流运输 (动态规划 SPFA 最短路)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...
随机推荐
- 详解MySQL中EXPLAIN解释命令(转)
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: expla ...
- 在SQL中导入Excel数据时强制以文本类型导入
Excel不是关系型数据库,在导入到sql中时对于数值型,sql有时int型会处理成float,有时数字文本混排的列,sql会认为是数值型,导入的结果有的数据变成了null,但是用sql导出excel ...
- ToString()字符转换类型
100.ToString("n");结果是100.00 100.ToString("c");结果是¥100.00 100.ToString("e&qu ...
- Vijos P1003 等价表达式 随机数+单调栈
题目链接:https://vijos.org/p/1003 题意: 1. 表达式只可能包含一个变量‘a’. 2. 表达式中出现的数都是正整数,而且都小于10000. 3. 表达式中可以包括四种运算‘+ ...
- 【viewResolver】 springmvc jsp
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> < ...
- Mac操作系统常用快捷键
复制:cmd+c 粘贴:cmd+v 剪切:先cmd+c,再cmd+opt+v 显示桌面:cmd+F3 切换输入法:cmd+space 打开Spotlight:ctrl+s ...
- SDC(4)–set_clock_groups 与–add选项
1,set_clock_groups -exclusive 有多个时钟,但是多个时钟不会同时生效 例如: 2,-add 只有一个时钟输入源,但是始终的频率等可能变 例如:
- 移动端Reactive Native轮播组件
移动端Reactive Native轮播组件 总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reac ...
- matlab怎么同时显示imshow 两幅图片
matlab怎么同时显示imshow 两幅图片 matlab怎么同时显示imshow 两幅图片 方法一:subplot()函数 subplot(2,1,1); subplot(2,1,2); 分上下或 ...
- eclipse, Log4j配置(真心的详细~)
转自: http://www.cnblogs.com/alipayhutu/archive/2012/06/21/2558249.html a). 新建Java Project>>新建pa ...