• P2176 [USACO14FEB]路障Roadblock


    题目描述

    每天早晨,FJ从家中穿过农场走到牛棚。农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度。FJ 的房子在 1 号田,牛棚在 N 号田。没有两块田被多条道路连接,以适当的路径顺序总是能在农场任意一对田间行走。当FJ从一块田走到另一块时,总是以总路长最短的道路顺序来走。

    FJ 的牛呢,总是不安好心,决定干扰他每天早晨的计划。它们在 M 条路的某一条上安放一叠稻草堆,使这条路的长度加倍。牛希望选择一条路干扰使得FJ 从家到牛棚的路长增加最多。它们请你设计并告诉它们最大增量是多少。

    输入输出格式

    输入格式:

    第 1 行:两个整数 N, M。

    第 2 到 M+1 行:第 i+1 行包含三个整数 A_i, B_i, L_i,A_i 和 B_i 表示道路 i 连接的田的编号,L_i 表示路长。

    输出格式:

    第 1 行:一个整数,表示通过使某条路加倍而得到的最大增量。

    输入输出样例

    输入样例#1: 复制
    5 7
    2 1 5
    1 3 1
    3 2 8
    3 5 7
    3 4 3
    2 4 7
    4 5 2
    输出样例#1: 复制
    2

    说明

    【样例说明】

    若使 3 和 4 之间的道路长加倍,最短路将由 1-3-4-5 变为 1-3-5。

    【数据规模和约定】

    对于 30%的数据,N <= 70,M <= 1,500。

    对于 100%的数据,1 <= N <= 100,1 <= M <= 5,000,1 <= L_i <= 1,000,000。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define inf 2147483647
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    #define ri register int
    template <class T> inline T min(T a, T b, T c)
    {
        return min(min(a, b), c);
    }
    template <class T> inline T max(T a, T b, T c)
    {
        return max(max(a, b), c);
    }
    template <class T> inline T min(T a, T b, T c, T d)
    {
        return min(min(a, b), min(c, d));
    }
    template <class T> inline T max(T a, T b, T c, T d)
    {
        return max(max(a, b), max(c, d));
    }
    #define scanf1(x) scanf("%d", &x)
    #define scanf2(x, y) scanf("%d%d", &x, &y)
    #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
    #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
    #define pi acos(-1)
    #define me(x, y) memset(x, y, sizeof(x));
    #define For(i, a, b) for (int i = a; i <= b; i++)
    #define FFor(i, a, b) for (int i = a; i >= b; i--)
    #define bug printf("***********
    ");
    #define mp make_pair
    #define pb push_back
    const int N = 10005;
    // name*******************************
    int n,m;
    int ans;
    int Head[N];
    int tot=1;//很神奇的操作
    struct edge
    {
        int to,next,w;
    } e[N];
    int dis[N];
    int pre[N];
    int egg[N];
    int vis[N];
    int rec[N];
    queue<int>que;
    // function******************************
    void add(int u,int v,int w)
    {
        e[++tot].to=v;
        e[tot].w=w;
        e[tot].next=Head[u];
        Head[u]=tot;
    }
    void spfa(int x)
    {
        me(dis,127);
    //me(vis,0)
        dis[x]=0;
        que.push(x);
        vis[x]=1;
        while(!que.empty())
        {
            int u=que.front();
            que.pop();
            vis[u]=0;
            for(int p=Head[u]; p; p=e[p].next)
            {
                int v=e[p].to;
                int w=e[p].w;
                if(dis[v]>dis[u]+w)
                {
                    dis[v]=dis[u]+w;
                    pre[v]=u;
                    egg[v]=p;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        que.push(v);
                    }
                }
            }
        }
    }
    
    
    //***************************************
    int main()
    {
    //    ios::sync_with_stdio(0);
    //    cin.tie(0);
    //    freopen("test.txt", "r", stdin);
        //  freopen("outout.txt","w",stdout);
        scanf("%d%d",&n,&m);
        For(i,1,m)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        spfa(1);
        int res=dis[n];
        int x=n;
    
        //这里必须提前存,直接这么遍历。。因为每走一次spfa,pre[x]的值都变了
        int cnt=0;
        while(x!=1)
        {
            rec[++cnt]=egg[x];
            x=pre[x];
        }
    
        For(i,1,cnt)
        {
            e[rec[i]].w*=2;
            e[rec[i]^1].w*=2;//完美处理双向图权值的改变
            spfa(1);
            ans=max(ans,dis[n]-res);
            e[rec[i]].w/=2;
            e[rec[i]^1].w/=2;
        }
        cout<<ans;
    
        return 0;
    }
  • 相关阅读:
    文字无缝滚动效果,鼠标移入时暂停
    Spring中使用@Autowired注解静态实例对象
    服务器环境搭建
    nexus问题
    useUnicode=true&characterEncoding=UTF-8 的作用
    SpringBoot项目启动时自动执行指定方法
    springboot自定义消息转换器HttpMessageConverter
    kubernetes资源类别介绍
    红黑树与平衡二叉树的比较
    Feign Client的超时时间
  • 原文地址:https://www.cnblogs.com/planche/p/8724687.html
Copyright © 2020-2023  润新知