zoukankan      html  css  js  c++  java
  • POJ 3662 Telephone Lines

    Telephone Lines
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7210   Accepted: 2634

    Description

    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.

    Input

    * Line 1: Three space-separated integers: N, P, and K
    * Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li

    Output

    * Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.

    Sample Input

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

    Sample Output

    4
    最短路+二分
    #include <iostream> 
    #include <algorithm> 
    #include <cstring> 
    #include <cstdio>
    #include <vector> 
    #include <queue> 
    #include <cstdlib> 
    #include <iomanip>
    #include <cmath> 
    #include <ctime> 
    #include <map> 
    #include <set> 
    using namespace std; 
    #define lowbit(x) (x&(-x)) 
    #define max(x,y) (x>y?x:y) 
    #define min(x,y) (x<y?x:y) 
    #define MAX 100000000000000000 
    #define MOD 1000000007
    #define pi acos(-1.0) 
    #define ei exp(1) 
    #define PI 3.141592653589793238462
    #define ios() ios::sync_with_stdio(false)
    #define INF 0x3f3f3f3f3f 
    #define mem(a) (memset(a,0,sizeof(a))) 
    typedef long long ll;
    struct Node
    {
        int x;
        int y;
        int l;
        friend bool operator<(const Node &a,const Node &b)
        {
            return a.l<b.l;
        }
    }node[100006];
    int a[10006][10006];
    int dis[10006];
    int vis[10006],n,m,k,x,y,z;
    void build(int l)
    {
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                a[i][j]=MOD;
            }
            a[i][i]=0;
        }
        for(int i=0;i<m;i++)
        {
            if(node[i].l<=l)
                a[node[i].x][node[i].y]=a[node[i].y][node[i].x]=0;
            else a[node[i].x][node[i].y]=a[node[i].y][node[i].x]=1;
        }
    }
    int disj(int u)
    {
        for(int i=1;i<=n;i++)
        {
            dis[i]=a[i][u];
            vis[i]=0;
        }
        int v,maxn;
        vis[u]=1;
        for(int i=2;i<=n;i++)
        {
            maxn=MOD;
            for(int j=1;j<=n;j++)
            {
                if(!vis[j] && dis[j]<maxn)
                {
                    v=j;
                    maxn=dis[j];
                }
            }
            if(maxn==MOD) break;
            vis[v]=1;
            for(int j=1;j<=n;j++)
            {
                if(!vis[j] && dis[j]>dis[v]+a[v][j])
                {
                    dis[j]=dis[v]+a[v][j];
                }
            }
        }
        return dis[n];
    }
    int bin(int k)
    {
        int l=0,r=m-1;
        while(l<=r)
        {
            int mid=l+r>>1;
            build(node[mid].l);
            if(disj(1)<=k) r=mid-1;
            else l=mid+1;
        }
        return l;
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].l);
        }
        sort(node,node+m);
        build(0);
        int ans=disj(1);
        if(ans==MOD) printf("-1
    ");
        else
        {
            if(ans<=k) printf("0
    ");
            else printf("%d
    ",node[bin(k)].l);
        }
        return 0;
    }
  • 相关阅读:
    event事件学习小节
    简单的碰壁反弹效果
    从全局中通过class类名获取标签
    js中常用的Tab切换
    将一串字符串转变为驼峰样式(字符串练习)
    js控住DOM实现发布微博简单效果
    使用js制作一般网站首页图片轮播效果
    使用js实现带有停顿效果的图片滚动(按钮控制)
    js实现标准无缝滚动
    toLowerCase和toLocaleLowerCase的区别
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7269130.html
Copyright © 2011-2022 走看看