zoukankan      html  css  js  c++  java
  • [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
    ",ans);
        else printf("%d
    ",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;
    }
  • 相关阅读:
    encodeURIcomponent编码和ASP.NET之间编码转换
    android入门学习一 基本概念
    Acvityity 的生命周期
    Android中ADT插件的安装
    控件必须放在具有 runat=server 的窗体标记内 错误解决解决方法
    AspNetPager 重写UrlRewriting配置示例
    将字符串按指定长度换行的一个C#方法
    [添砖加瓦]:ExtJS+WCF+LINQ打造全功能Grid
    C#中判断字符是否为中文
    Asp.net中防止用户多次登录的方法
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7498774.html
Copyright © 2011-2022 走看看