Roadblock

2428: Roadblock

时间限制: 1 Sec  内存限制: 64 MB
提交: 17  解决: 6
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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.

思路:改变的一定是最短路上面的(否则仍然走最短路就好了)。其次我们记录最短路,枚举改变的边,取最大值,再减去原来的最短路径长度。

#include<iostream>
#include<cstring>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
#define PER(i, a, b) for(int i = (a); i >= (b); -- i)
using namespace std;
const int maxn = 5100;
template <class T>
inline void rd(T &ret){
    char c;
    ret = 0;
    while ((c = getchar()) < ‘0‘ || c > ‘9‘);
    while (c >= ‘0‘ && c <= ‘9‘){
        ret = ret * 10 + (c - ‘0‘), c = getchar();
    }
}
struct edge{
     int v,w,next;
}p[maxn*2];
int n,m,cnt=1,d[maxn],ans,head[maxn],vis[maxn],cur,fa[maxn],e[maxn],w[maxn];
void add(int u,int v,int w){
    p[++cnt].v=v,p[cnt].w=w,p[cnt].next=head[u],head[u]=cnt;
}
queue<int>que;
void spfa(int s){
    memset(vis,0,sizeof(vis));
    memset(d,127,sizeof(d));
    que.push(s);
    vis[s]=1;
    d[s]=0;
    while(que.size()){
        int cur=que.front();
        que.pop();
        vis[cur]=0;
        for(int i=head[cur];i;i=p[i].next){
            int to=p[i].v;
            if(d[to]>d[cur]+p[i].w){
                d[to]=d[cur]+p[i].w;
                fa[to]=cur;
                e[to]=i;
                if(!vis[to]){
                    que.push(to);
                    vis[to]=1;
                }
            }
        }
    }
}

int main() {
    rd(n),rd(m);
    REP(i, 1, m){
         int u,v,w;
         cin>>u>>v>>w;
         add(u,v,w),add(v,u,w);
    }
    spfa(1);
    ans=d[n];
    int r=0;
    for(int i=n;i;i=fa[i])w[++r]=e[i];
    REP(i, 1, r){
         p[w[i]].w*=2,p[w[i]^1].w*=2;
         spfa(1);
         cur=max(cur,d[n]);
         p[w[i]].w/=2,p[w[i]^1].w/=2;
    }
    cout<<cur-ans<<endl;
}

原文地址:https://www.cnblogs.com/czy-power/p/10364483.html

时间: 2024-10-10 10:12:24

Roadblock的相关文章

(寒假集训)Roadblock(最短路)

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

BZOJ 3445: [Usaco2014 Feb] Roadblock

Description 一个图, \(n\) 个点 \(m\) 条边,求将一条边距离翻倍后使 \(1-n\) 最短路径增加的最大增量. Sol Dijstra. 先跑一边最短路,然后枚举最短路,将路径翻倍然后跑Dijstra... 因为不在最短路径上的边没用贡献,然后最短路径最长为 \(n-1\) 复杂度 \(O(nmlogm\) Code /************************************************************** Problem: 3445

[USACO14FEB]路障Roadblock

题目:洛谷P2176. 题目大意:有n个点m条无向边,一个人要从1走到n,他会走最短路.现在可以让一条边的长度翻倍,求翻倍后这个人要多走多少距离. 解题思路:首先可以知道,翻倍肯定是在最短路上的某条边翻,否则他走的路不会变.我们先跑一遍最短路,记录下走的边,再枚举哪条边翻倍,然后跑最短路,记录下答案即可. 此题好像卡SPFA,于是我用堆优化Dijkstra秒杀. 时间复杂度$O(nm\log n)$. C++ Code: #include<cstdio> #include<cstring

P2176 [USACO14FEB]路障Roadblock

题目传送门 十分值得一做的最短路,题目意思十分明确,一条边权值加倍后最多比加倍前的最短路花费多多少.首先看到m<=5000,第一念头就是跑m遍最短路,但是会严重超时,实际上是由于有些边的权值改变,对最短路没有造成任何影响,才导致了我们程序的严重超时,所以我们采取第一次跑最短路记路径的方法.开三个辅助数组path[].pre[]和edge[],path[]就是用来记最短路经过了哪些编号的边(邻接表存边),pre[i]表示走i号节点之前走的边,edge[i]表示到i号节点之前所经过的点,然后whil

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

越狱Season 1- Episode 22: Flight

Season 1, Episode 22: Flight -Franklin: You know you got a couple of foxes in your henhouse, right? fox: 狐狸 henhouse: 鸡舍 你的队伍里都是一群狐狸 -Michael: They both want out of here. both: 两者都 他们都想出去 They'll behave until then. behave: 举止端正 出去前都会安分的 -Franklin: Lo

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?