• Luogu P1948 [USACO08JAN]电话线Telephone Lines(最短路+dp)


    P1948 [USACO08JAN]电话线Telephone Lines

    题意

    题目描述

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.

    There are (N(1 leq N leq 1,000)) forlorn telephone poles conveniently numbered (1 cdots N) that are scattered around Farmer John's property; no cables connect any them. A total of (P(1 leq P leq 10,000)) pairs of poles can be connected by a cable; the rest are too far apart.

    The i-th cable can connect the two distinct poles (A_i) and (B_i), with length (L_i(1 leq L_i leq 1,000,000)) units if used. The input data set never names any ({ Ai, Bi }) pair more than once. Pole (1) is already connected to the phone system, and pole (N) is at the farm. Poles (1) and (N) need to be connected by a path of cables; the rest of the poles might be used or might not be used.

    As it turns out, the phone company is willing to provide Farmer John with (K(0 leq K < N)) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or (0) if he does not need any additional cables.

    Determine the minimum amount that Farmer John must pay.

    多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着(N(1 leq N leq 1000))根据(1 cdots n)顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有(p(1 leq p leq 10000))对电话杆可以拉电话线。其他的由于地震使得无法连接。

    (i)对电线杆的两个端点分别是(a_i,b_i),它们的距离为(l_i(1 leq l_i leq 1000000))。数据中每对((a_i,b_i))只出现一次。编号为(1)的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号(N)的电话线杆上。也就是说,笨笨的任务仅仅是找一条将(1)号和(N)号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。

    电信公司决定支援灾区免费为此市连接(k)对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过(k)对,那么支出为(0)

    请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?

    输入输出格式

    输入格式:

    输入文件的第一行包含三个数字(n,p,k);

    第二行到第(p+1)行,每行分别都为三个整数(a_i,b_i,l_i)

    输出格式:

    一个整数,表示该项工程的最小支出,如果不可能完成则输出(-1)

    输入输出样例

    输入样例:

    5 7 1
    1 2 5
    3 1 4
    2 4 8
    3 2 3
    5 2 9
    3 4 7
    4 5 6
    

    输出样例:

    4
    

    思路

    (solo)! --Mercury

    (solo)又赢了水星 祭。

    这道题水星打的二分,直接二分答案,确实很简单(然而他现在还在调试)。在这里给一个动态规划的思路。

    在一般的最短路中,我们用数组(dis[v])表示起点到点(v)的最短路。我们把这个数组再开一维,用(dis[v][i])表示已经消除了(i)条边的价格之后到达(v)点的最短路。那么在松弛一条边((u,v))的时候我们就有以下转移策略:

    • 直接转移到下一个点,也就是(dis[v][i]=max( ext{那条边的长度},dis[u][i]))
    • 这条边的价格消除为零,也就是(dis[v][i+1]=max( ext{那条边的长度},dis[u][i]))

    然后我们再套上一个(SPFA)或者(Dijkstra)就能过了。

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int,int> PII;
    const int MAXN=1005;
    const int MAXM=20005;
    int n,m,k,f[MAXN][MAXN];
    int cnt,top[MAXN],to[MAXM],len[MAXM],nex[MAXM];
    bool vis[MAXN][MAXN];
    int read()
    {
        int re=0;
        char ch=getchar();
        while(!isdigit(ch)) ch=getchar();
        while(isdigit(ch)) re=(re<<3)+(re<<1)+ch-'0',ch=getchar();
        return re;
    }
    void SPFA()
    {
        memset(f,0x3f,sizeof f);
        f[1][0]=0;
        queue<PII>Q;
        Q.push(make_pair(1,0));
        while(!Q.empty())
        {
            int now=Q.front().first,dep=Q.front().second;Q.pop();
            vis[now][dep]=false;
            for(int i=top[now];i;i=nex[i])
            {
                if(f[to[i]][dep]>max(f[now][dep],len[i]))
                {
                    f[to[i]][dep]=max(f[now][dep],len[i]);
                    if(!vis[to[i]][dep])
                    {
                        vis[to[i]][dep]=true;
                        Q.push(make_pair(to[i],dep));
                    }
                }
                if(dep<k&&f[to[i]][dep+1]>f[now][dep])
                {
                    f[to[i]][dep+1]=f[now][dep];
                    Q.push(make_pair(to[i],dep+1));
                }
            }
        }
    }
    int main()
    {
        n=read(),m=read(),k=read();
        while(m--)
        {
            int x=read(),y=read(),z=read();
            to[++cnt]=y,len[cnt]=z,nex[cnt]=top[x],top[x]=cnt;
            to[++cnt]=x,len[cnt]=z,nex[cnt]=top[y],top[y]=cnt;
        }
        SPFA();
        printf("%d",f[n][k]==0x3f3f3f3f?-1:f[n][k]);
        return 0;
    }
    
  • 相关阅读:
    1388:Lake Counting
    1253 Dungeon Master
    Ubuntu18.04下可以完美运行Quake3 Arena
    Windows下python3生成UTF8的CSV文件和sha256sum踩坑记录
    ROM后缀含义
    Ubuntu18.04下的模拟神器RetroArch
    廉价的SUP掌机拆解
    Python3连接MySQL
    Ubuntu18.04的网络管理netplan和防火墙ufw
    Ubuntu18.04命令行连接WiFi
  • 原文地址:https://www.cnblogs.com/coder-Uranus/p/9765912.html
Copyright © 2020-2023  润新知