Roadblock

Posted protagonist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于Roadblock的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 3445: [Usaco2014 Feb] Roadblock

[USACO14FEB]路障Roadblock

[USACO14FEB]路障Roadblock

P2176 [USACO14FEB]路障Roadblock

P2176 [USACO14FEB]路障Roadblock

微信小程序代码片段