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

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

这题很好

 #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. JAVA学习资料整理

    今天偶然间发现之前一个群里发过的一篇关于JAVA学习资料的东西.本着服务大众的精神,搬来了博客园: <JAVA编程思想>第四版(英文原版) 下载地址:http://115.com/file ...

  2. Linux启用MySQL的InnoDB引擎

    前几天公司的一个项目组的同事反应说公司内部的一台Linux服务器上的MySQL没有InnoDB这个引擎,我当时想应该不可能啊,MySQL默认应该 就已经安装了这个引擎的吧,于是上服务器去看了看,发现还 ...

  3. PythonCrawl自学日志

    2016-09-10 PythonCrawl自学日志 1.python及Selenium的安装 (1)开发环境使用的是VS2015 Community.python3.5.Selenium3.0BET ...

  4. Tips of Python!

    Tips of Python!(Python 2.7) (不定期更新中-) 1. raw_input() 和 input(): raw_input() 将输入原封不动的保存为一个字符串 输入 1 + ...

  5. vi/vim正则表达式

    http://www.cnblogs.com/penseur/archive/2011/02/25/1964522.html 毋庸多言,在vim中正则表达式得到了十分广泛的应用. 最常用的 / 和 : ...

  6. Java集合Map接口与Map.Entry学习

    Java集合Map接口与Map.Entry学习 Map接口不是Collection接口的继承.Map接口用于维护键/值对(key/value pairs).该接口描述了从不重复的键到值的映射. (1) ...

  7. 在树莓派上部署asp.net

    今天成功的在树莓派上部署asp.net呢.之前在unbuntu上测试成功了,结果今天操作的时候又不会操作了,主要对Linux太不熟悉了,找资料,资料又不多,这次赶紧记录下来,以备下次查阅. 我用的mo ...

  8. sencha touch json store

    js: Ext.define('MyApp.store.MyJsonStore', { extend: 'Ext.data.Store', requires: [ 'MyApp.model.Perso ...

  9. javascript随手记

    编码规范 避免使用全局变量 写在所有函数外面的变量就是全局变量. 之所以要避免使用全局变量是因为:如果有多个类库的话,它们都定义了一个名字的变量.这时候后引入的类库中该变量的值就会覆盖前面引入的类库中 ...

  10. python 进程信息

    通过psutil模块读取机器进程信息: #-*- coding: UTF-8 -*-import psutil;import osimport CommMethod '''获取机器当前进程信息'''d ...