Roadblock

时间限制: 1 Sec  内存限制: 64 MB
提交: 9  解决: 5
[提交][状态][讨论版]

题目描述

Every
morning, FJ wakes up and walks across the farm from his house to the
barn.  The farm is a collection of N fields (1 <= N <= 250)
connected by M bidirectional pathways (1 <= M <= 25,000), each
with an associated length.
FJ's house is in field 1, and the barn is
in field N.  No pair of fields is joined by multiple redundant pathways,
and it is possible to travel between any pair of fields in the farm by
walking along an appropriate sequence of pathways. 
When traveling from one field to another, FJ always selects a route
consisting of a sequence of pathways having minimum total length.

Farmer John's cows, up to no good as
always, have decided to interfere with his morning routine.  They plan
to build a pile of hay bales on exactly one of the M pathways on the
farm, doubling its length.  The cows wish to select
a pathway to block so that they maximize the increase in FJ's distance
from the house to the barn.  Please help the cows determine by how much
they can lengthen FJ's route.

输入

* Line 1: Two space-separated integers, N and M.
* Lines 2..1+M: Line j+1 describes the jth
bidirectional pathway in terms of three space-separated integers: A_j
B_j L_j, where  A_j and B_j are indices in the range 1..N indicating
the  fields joined by the pathway, and L_j is the length of the pathway
(in the range 1...1,000,000).

输出

*
Line 1: The maximum possible increase in the total length of FJ's
shortest route made possible by doubling the length of a  single
pathway.

样例输入

5 7
2 1 5
1 3 1
3 2 8
3 5 7
3 4 3
2 4 7
4 5 2

样例输出

2

提示

There are 5 fields and 7 pathways.  Currently, the shortest path from the house (field 1) to the barn (field 5) is 1-3-4-5 of total length 1+3+2=6.If the cows double the length of the pathway from field 3 to field 4 (increasing its length from 3 to 6), then FJ's shortest route is now 1-3-5, of total length 1+7=8, larger by two than the previous shortest route length.

【分析】农夫要从1走到n,他只走最短路。现在他的牛想使坏,想在一些路上设置障碍,使得这条路的长度变为2倍,求最短路的最大增量(农夫选择的)。先跑一边最段路,找前驱,然后将前驱所练成的每一条边依次遍历将其扩大二倍,再在只把该边扩大二倍的原图上跑最短路,找d[n]的最大值。可能存在很多的最短路,但是只用处理一条,因为即使有两条不相交的最短路,那么跑完之后d[n]还跟第一次跑的一样,不影响。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e3;
const int M = ;
int n,m,k;
int edgg[N][N];
int edg[N][N],vis[N],d[N],pre[N];
void spfa(int x)
{
met(vis,);
met(d,inf);
d[]=;
vis[]=;
queue<int>q;
q.push();
while(!q.empty()){
int t=q.front();q.pop();
vis[t]=;
for(int i=;i<=n+;i++){
if(d[i]>d[t]+edg[t][i]){
d[i]=d[t]+edg[t][i];
if(!x)pre[i]=t;
if(!vis[i])q.push(i),vis[i]=;
}
}
}
}
int main()
{
met(edg,inf);
met(pre,-);
int ans1,ans2=;
scanf("%d%d",&n,&m);
int u,v,w;
while(m--){
scanf("%d%d%d",&u,&v,&w);
edg[u][v]=edg[v][u]=w;
}
spfa();
ans1=d[n];
for(int i=n;pre[i]!=-;i--){
int v=pre[i];
edg[i][v]*=;
edg[v][i]*=;
spfa();
ans2=max(ans2,d[n]);
edg[i][v]/=;
edg[v][i]/=;
}
printf("%d\n",ans2-ans1);
return ;
}

(寒假集训)Roadblock(最短路)的更多相关文章

  1. CSU-ACM寒假集训选拔-入门题

    CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...

  2. 「BZOJ3694」「FJ2014集训」最短路

    「BZOJ3694」「FJ2014集训」最短路 首先树剖没得说了,这里说一下并查集的做法, 对于一条非树边,它会影响的点就只有u(i),v(i)到lca,对于lca-v的路径上所有点x,都可通过1-t ...

  3. HZNU-ACM寒假集训Day4小结 最短路

    最短路 1.Floy 复杂度O(N3)  适用于任何图(不存在负环) 模板 --kuangbin #include<iostream> #include<cstdio> #in ...

  4. (寒假集训) Piggyback(最短路)

    Piggyback 时间限制: 1 Sec  内存限制: 64 MB提交: 3  解决: 3[提交][状态][讨论版] 题目描述 Bessie and her sister Elsie graze i ...

  5. 中南大学2019年ACM寒假集训前期训练题集(基础题)

    先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...

  6. 【寒假集训系列DAY.1】

    Problem A. String Master(master.c/cpp/pas) 题目描述 所谓最长公共子串,比如串 A:“abcde”,串 B:“jcdkl”,则它们的最长公共子串为串 “cd” ...

  7. 2022寒假集训day2

    day1:学习seach和回溯,初步了解. day2:深度优化搜索 T1 洛谷P157:https://www.luogu.com.cn/problem/P1157 题目描述 排列与组合是常用的数学方 ...

  8. GlitchBot -HZNU寒假集训

    One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a lis ...

  9. Wooden Sticks -HZNU寒假集训

    Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...

随机推荐

  1. web自动化测试,定位不到元素的原因及解决方案(持续更新中2018年9月29日)

    主要讲自己在实战中遇到的坑: 1.动态id定位不到元素 分析原因:每次打开页面,ID都会变化.用ID去找元素,每次刷新页面ID都会发生变化. 解决方案:推荐使用xpath的相对路径方法或者cssSel ...

  2. Linux认知之旅【02 装个软件玩玩】!

    〇.命令行终端熟悉了吗? 1.没有仔细研究上一篇文章? 拿上看看这几个命令:ls.cd.cp.mv.rm.mkdir.touch.cat.less.恩,暂时这些够用了! 什么?你连虚拟机也没装! 感谢 ...

  3. Python全栈工程师(异常(基础))

    ParisGabriel                每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰     Python人工智能从入门到精通 补充:包的相对导入 只对后两种导入方式有用 ...

  4. 椭圆曲线加密和rsa对比

    最近在导师的要求下接手了基于欧洲标准的车联网项目中的安全层,需要学习密码学,以及网络安全的相关内容,这里做一个总结 引用的大部分内容为一个西安的大佬(哈哈我老家也是西安的),大佬主页:https:// ...

  5. 系统编程--文件IO

    1.文件描述符 文件描述符是一个非负整数,当打开一个现有文件或创建一个新文件时候,内核向进程返回一个文件描述符,新打开文件返回文件描述符表中未使用的最小文件描述符.Unix系统shell使用文件描述符 ...

  6. 在LinkedIn的 Kafka 生态系统

    在LinkedIn的 Kafka 生态系统 Apache Kafka是一个高度可扩展的消息传递系统,作为LinkedIn的中央数据管道起着至关重要的作用. Kafka 是在2010年在LinkedIn ...

  7. easyui中tab页中js脚本无法加载的问题及解决方法

    我发现tab页中<script src="xxx.js">方式加载的脚本没有生效,firebug看请求也没有请求相应的脚本文件. 单独在浏览器中打开tab页中的页面js ...

  8. thinkPHP判断是否修改成功

    thinkPHP中使用save方法来更新数据的save方法的正常执行时返回值是影响的记录数,出错时返回false,返回为0和返回false在很多业务场景下都是不同的. 而当修改的内容和原有内容一致的时 ...

  9. 【转】oracle 删除重复记录

    转至:http://blog.163.com/aner_rui/blog/static/12131232820105901451809/ 2.保留一条(这个应该是大多数人所需要的 ^_^) Delet ...

  10. [AtCoder ARC093F]Dark Horse

    题目大意:有$2^n$个人,每相邻的两个人比赛一次.令两个人的编号为$a,b(a\leqslant b)$,若$a\neq 1$,则$a$的人获胜:否则若$b\in S$则$b$获胜,不然$1$获胜. ...