Poj 3662 Telephone Lines【二分+SPFA】

Telephone Lines

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 6157

 

Accepted: 2260

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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

Source

USACO 2008 January Silver

 

题目大意:有p条电缆,目的是建一条最小花费电缆,使得1能够到达n。然后官方给你提供K条电缆(让你免费建k条边),剩下的电缆我们自己拿钱,问我们自己拿钱中的那条最贵的电缆最小是多少。


思路:


1、二分查找答案,对于当前解,如果可行,ans=mid,继续二分,如果不可行,也继续二分,直到不能二分为止。


2、对于当前确定的mid值,建图的过程中,将值大于mid的边权设定为1 ,小于mid的边权设定为0、这时候求一遍最短路,dis【n】其实就表示为:我们自己拿钱中的那条边最贵是mid,剩下的需要公式来提供dis【n】条电缆,显然,如果dis【n】>k是不行的,反之,就是可行解。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,p,k;
int head[100000];
struct node
{
    int x,y,w;
}a[1000000];
struct EdgeNode
{
    int to;
    int w;
    int next;
}e[100000];
int dis[100000];
int vis[100000];
int cont;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
int SPFA()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
    dis[1]=0;
    vis[1]=1;
    queue<int >s;
    s.push(1);
    while(!s.empty())
    {
        int u=s.front();
        s.pop();vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    if(dis[n]>k)return 0;
    else return 1;
}
int Slove(int mid)
{
    cont=0;
    memset(head,-1,sizeof(head));
    for(int i=0;i<p;i++)
    {
        if(a[i].w>mid)
        {
            add(a[i].x,a[i].y,1);
            add(a[i].y,a[i].x,1);
        }
        else
        {
            add(a[i].x,a[i].y,0);
            add(a[i].y,a[i].x,0);
        }
    }
    if(SPFA()==1)return 1;
    else return 0;
}
int main()
{
    while(~scanf("%d%d%d",&n,&p,&k))
    {
        for(int i=0;i<p;i++)
        {
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
        }
        int l=0;
        int r=100000000;
        int mid;
        int ans=-1;
        while(r>=l)
        {
            mid=(l+r)/2;
            if(Slove(mid)==1)
            {
                r=mid-1;
                ans=mid;
            }
            else l=mid+1;
        }
        printf("%d\n",ans);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值