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 ...
随机推荐
- Boost format
浅尝boost之format 概述 std::string是个很不错的东东,但实际使用时基本在每个程序里都会遇到不愉快的事情:格式化字符串.我甚至由于这个原因在代码里引入平台有关的MFC,A ...
- hdu 3549 Flow Problem Edmonds_Karp算法求解最大流
Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...
- Eclipse启动Tomcat错误:Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already(转载)
转载自:http://blog.csdn.net/aigochina/article/details/7891107 Eclipse启动Tomcat错误: Several ports (8080, 8 ...
- 4.MVC框架开发(母版页的应用、按钮导致的Action处理、从界面向控制器传数据和HtmlHelper控件的实现(注册的实现))
1.在视图里如何引入母版页 1)在视图里母版页都是放在View目录下面的Shared文件夹下面 2)母版页里的RenderBody()类似于ASP.NET里面的ContentPalceHolder占位 ...
- AForm — 模型驱动的自动化表单解决方案
http://xiehuiqi220.github.io/AForm/doc/book/#
- 如何避免远程循环执行SSH时,到第一条之后就退出
RT 今天遇到的小问题,记录下来. 据说关键是加 < /dev/null 我也是看网上别人说的,测试成功. #!/bin/bash while read line do echo $line ...
- NHibernate与EF(Entity Framework)的区别
http://www.cnblogs.com/lukun/archive/2011/05/16/2047587.html NHibernate与EF(Entity Framework)的区别 http ...
- Pascal Game Development with Jason McMillen
In this much belated episode I talk with Jason McMillen of Pascal Game Development. We discuss the s ...
- 自定义的IntentFileter 无法找到activity
<intent-filter > <action android:name="com.leo.enjoytime.VIEW"/></intent-fi ...
- JQuery实现点击div以外的位置隐藏该div窗口
简单示例代码: <body> <script type="text/javascript" src="http://ajax.googleapis.co ...