[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 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John‘s property; no cables connect any them. A total of P (1 ≤ P ≤ 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 Ai and Bi, with length Li (1 ≤ Li ≤ 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 ≤ 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<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。

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

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

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

输入输出格式

输入格式:

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

第二行到第p+1行,每行分别都为三个整数ai,bi,li。

输出格式:

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

输入输出样例

输入样例#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

输出样例#1:

4

题解:  是一到好题,首先看到这个题目肯定大家先想到最小瓶颈生成树,打了一下,发现wa了,原因是因为他只是包含最大的边,次大的边不一定包含。  所有转换思路,考虑如果没有那k次免费机会就可以二分加并查集来做了,但是有k次免费的机会,在此之上,考虑先二分加并查集,把边权<=mid的边先加进去,就形成了若干个联通块。  然后我们考虑将每个联通块看成一个点,把>mid的边加进去,(边权为1),跑最短路,如果到n所在的联通块的距离>k,那么就需要大于k条边,就不行,小的话,就代表可以在k条边之内解决,这就是check。代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#define ll long long
#define MAXN 10100
using namespace std;
struct edge{
    int first;
    int next;
    int to;
    int quan;
}a[MAXN*2];
struct edge2{
    int from,to,quan;
    void read(){
        scanf("%d %d %d",&from,&to,&quan);
    }
}e[MAXN*2];
int fa[MAXN];
int dis[MAXN],have[MAXN];
int n,m,k,maxx=0,num=0;
queue<int> q;

int find(int now){
    if(fa[now]!=now) fa[now]=find(fa[now]);
    return fa[now];
}

void addedge(int from,int to,int quan){
    a[++num].to=to;
    a[num].quan=quan;
    a[num].next=a[from].first;
    a[from].first=num;
}

int spfa(){
    memset(have,0,sizeof(have));
    memset(dis,127,sizeof(dis));
    while(!q.empty()) q.pop();
    dis[fa[1]]=0,have[fa[1]]=1,q.push(fa[1]);
    while(!q.empty()){
        int now=q.front();
        q.pop();
        have[now]=0;
        for(int i=a[now].first;i;i=a[i].next){
            int to=a[i].to,quan=a[i].quan;
            if(dis[to]>dis[now]+quan){
                dis[to]=dis[now]+quan;
                if(!have[to]){
                    have[to]=1;
                    q.push(to);
                }
            }
        }
    }
    return dis[fa[n]];
}

bool check(int cant){
    memset(a,0,sizeof(a));
    //for(int i=1;i<=num;i++) a[i].quan=0,a[i].first=0,a[i].to=0,a[i].next=0;num=0;
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++){
        if(e[i].quan<=cant){
            int x=find(e[i].from),y=find(e[i].to);
            if(fa[x]!=fa[y]) fa[x]=y;
        }
    }
    for(int i=1;i<=n;i++) find(i);
    if(fa[1]==fa[n]) return 1;
    for(int i=1;i<=m;i++){
        if(e[i].quan>cant){
            int x=e[i].from,y=e[i].to;
            if(fa[x]!=fa[y]) {
                addedge(fa[x],fa[y],1);
                addedge(fa[y],fa[x],1);}
        }
    }
    int d=spfa();
    if(d<=k) return 1;
    return 0;
}

void work(){
    int l=0,r=maxx,mid,ans=-1;
    while(l<=r){
        mid=(l+r)/2;
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    if(ans==-1) printf("%d\n",ans);
    else printf("%d\n",ans);
}

int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1;i<=m;i++) e[i].read(),maxx=max(maxx,e[i].quan);
    work();
    return 0;
}
时间: 2024-07-29 23:26:46

[USACO08JAN]电话线Telephone Lines的相关文章

洛谷 P1948 [USACO08JAN]电话线Telephone Lines

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 a

P1948 [USACO08JAN]电话线Telephone Lines

P1948 [USACO08JAN]电话线Telephone Lines 题目描述 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人.该市周围分布着N(1<=N<=1000)根据1--n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线.其他的由于地震使得无法连接. 第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)

【洛谷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 ≤ N ≤ 1,000) forlorn telephon

题解 guP1948 【[USACO08JAN]电话线Telephone Lines】

二分+dij题目 首先读一遍题目,一定要仔细读(不要问我为什么,我就是受害者qwq 你会发现最终的费用是由最长的电话线决定的,而非电话线长度和. 至此就有了一个基本思路--枚举(二分)出可能的最长电话线长度,然后对其进行dij判断. dij思路如下: 1.已知枚举出了假定答案ans: 2.在最短路过程中,判断有多少条线长度大于ans,并将其免费: 3.最后判断免费条数,若大于给出的t即不可行,反之可行. 开long long!!! 代码: #include<bits/stdc++.h> usi

P1948 [USACO08JAN]电话线Telephone Lines spfa 二分答案

多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人.该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线.其他的由于地震使得无法连接. 第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000).数据中每对(ai,bi)只出现一次.编号为1的电话杆已经接入了全国的电话网络,整个

「USACO08JAN」电话线Telephone Lines 解题报告

题面 大意:在加权无向图上求出一条从 \(1\) 号结点到 \(N\) 号结点的路径,使路径上第 \(K + 1\) 大的边权尽量小. 思路: 由于我们只能直接求最短路,不能记录过程中的具体的边--那样会特别麻烦 所以,我们就尝试着去想更优的办法 题目中所说,能够免去 \(K\) 条边的费用,那么对于要建设的边中,肯定免去费用最大 $K $ 条边更优 我们关注的应该是第 \(K+1\) 大边的边权,因为其他边权大于这条边边权的边都会被略去 这条边可以枚举吗? 似乎不行,枚举的复杂度还要在最短路的

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废

bzoj1614[Usaco2007 Jan]Telephone Lines架设电话线*

bzoj1614[Usaco2007 Jan]Telephone Lines架设电话线 题意: n个节点,1号节点已经连入互联网,现在需要将整个图连入网络.有K条边可以免费连接,最后总费用为所有连边费用的最大值,求最小总费用.n≤10000 题解: 二分费用,将连边费用大于二分值的长度记为1,否则记为0,求最短路,如果到某个点的距离超过k,则需要增加答案,继续二分. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include &l

USACO08JAN Telephone Lines 题解

1.USACO08JAN  Telephone Lines 题面 由于问的是最大值最小,所以二分加验证就好了 比较显然的,题干问的是第k+1长的路最短: 那么二分答案是正确的方向: 但是怎么验证? 我们可以将所有边权大于二分的答案的边视为边权是1,否则看成0: 然后从1~n跑最短路,如果答案大于二分的答案那么就不成立,否则成立: 这种思维比较重要,代码还是很简单的: #include <bits/stdc++.h> using namespace std; int head[2000010],