求最短路径覆盖的全部边权值和。

思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径。

这题很好

 #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最短路)的更多相关文章

  1. UVALive 6885 Flowery Trails 最短路

    Flowery Trails 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid= ...

  2. NOIP2013 华容道 (棋盘建图+spfa最短路)

    #include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...

  3. UVALive 6885 Flowery Trails 最短路枚举

    题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...

  4. poj1502 spfa最短路

    //Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...

  5. HNU 12847 Dwarf Tower(最短路+队列优化)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847 解题报告:有n样物品,编号从1到n第i样物品可以通过金 ...

  6. hiho(1081),SPFA最短路,(非主流写法)

    题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...

  7. hdu 5545 The Battle of Guandu spfa最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...

  8. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  9. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

随机推荐

  1. 调试NodeJS应用

    OS:Windows 1.下载安装NodeJS 点击http://nodejs.org/界面上“Install”,下载后运行安装,默认安装到到C:\Program Files\nodejs.安装后确认 ...

  2. css helper class

    应该习惯的css helper class .text-centered text-align: center; .text-right text-align: right; .small small ...

  3. Java 中正确使用 hashCode 和 equals 方法

    在这篇文章中,我将告诉大家我对hashCode和equals方法的理解.我将讨论他们的默认实现,以及如何正确的重写他们.我也将使用Apache Commons提供的工具包做一个实现. 目录: hash ...

  4. ios App优化

    一. 静态分析(Analyze) 在Xcode菜单栏中点击 ”Product“ -> "Analyze",编译完成后项目工程中可能造成内存泄露的代码就会被标记出来,这样我们就 ...

  5. UIWebView 加载网页、文件、 html-b

    UIWebView  是用来加载加载网页数据的一个框.UIWebView可以用来加载pdf word doc 等等文件 生成webview 有两种方法,1.通过storyboard 拖拽 2.通过al ...

  6. HAProxy 的负载均衡服务器,Redis 的缓存服务器

    问答社区网络 StackExchange 由 100 多个网站构成,其中包括了 Alexa 排名第 54 的 StackOverflow.StackExchang 有 400 万用户,每月 5.6 亿 ...

  7. bzoj 2406: 矩阵 上下界网络流判定

    2406: 矩阵 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 46[Submit][Status][Discuss] De ...

  8. 用 Maven 做项目构建

    转自:http://www.ibm.com/developerworks/cn/java/j-lo-maven/index.html 本文将介绍基于 Apache Maven 3 的项目构建的基本概念 ...

  9. ANGULAR 2 FOR REACT DEVELOPERS

    Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...

  10. 关于JDNI、JMX

    http://www.cnblogs.com/itech/archive/2010/09/16/1827999.html http://javacrazyer.iteye.com/blog/75948 ...