POJ3662 Telephone Lines (dijkstra+二分)

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 <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
int n,p,k;
int d[1005];
int head[1005],ver[20005],edge[20005],Next[20005];//开两倍存储双向边
bool v[1005];
int tot=0;
priority_queue<pair<int,int> >q;
void add(int x,int y,int z)
{
    ver[++tot]=y;
    edge[tot]=z;
    Next[tot]=head[x];
    head[x]=tot;
}
int dijkstra(int mid)//花费小于等于mid记为0 否则记为1 //只需要为一条路买单 //dij返回的是大于mid的数
{
    int cnt=0;
    memset(d,0x3f,sizeof(d));
    memset(v,0,sizeof(v));
    d[1]=0;
    q.push(make_pair(0,1));
    int i,j;
    while(q.size())
    {
        int x=q.top().second;
        q.pop();
        if(v[x])continue;
        v[x]=1;
        for(i=head[x];i;i=Next[i])
        {
            int y=ver[i],z=edge[i];
            int z1;
            if(z<=mid)z1=0;
            else z1=1;
            if(d[y]>d[x]+z1)// 注意 这里要求的最短路并不是原来费用的最短路
            {
                d[y]=d[x]+z1;
                q.push(make_pair(-d[y],y));
            }
        }
    }
    return d[n];
}
bool check(long long mid)
{
    if(dijkstra(mid)<=k)
    {
        return true;
    }
    else return false;
}
int main()
{
    int i;
    memset(v,0,sizeof(v));
    scanf("%d%d%d",&n,&p,&k);
    for(i=1;i<=p;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    long long l=0,r=1000002,mid;
    while(l<r)
    {
        mid=(l+r)>>1;
        if(check(mid))//钱数够了缩小
        {
            r=mid;
        }
        else//钱数不够扩大
        {
            l=mid+1;
        }
    }
    if(r>1000000)cout<<-1<<endl;
    else if(r<0)cout<<0<<endl;
    else cout<<l<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/lipoicyclic/p/12293686.html

时间: 2024-10-13 02:01:30

POJ3662 Telephone Lines (dijkstra+二分)的相关文章

POJ3662 Telephone Lines( dijkstral + 二分 )

POJ3662 Telephone Lines 题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的, 求这m-k条边中花费最大的一条边的最小花费. 让m条边中原本花费最大的k条边成为免费的边,则这时m-k条边中花费最大的一条边的花费最小. 二分枚举m-k条边中花费最大的一条边的最小花费x,dijkstra求最短路径时,将花费大于x的边的花费设为1(花费为INF的边不变),花费小于等于x的边设为 0,则d[v-1]中返回的就是花费大于x

poj3662 Telephone Lines【最短路】【二分】

http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:9310   Accepted: 3374 Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative,

poj 3662 Telephone Lines dijkstra+二分搜索

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 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

USACO 2008 JAN Telephone Lines 【二分答案、SPFA】

题目 题目描述 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 telep

POJ 3662 Telephone Lines(二分答案+SPFA)

[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求最小费用. [题解] 二分答案,对于大于二分答案的边权置为1,小于等于的置为0, 则最短路就是超出二分答案的线数,如果小于等于k,则答案是合法的 [代码] #include <cstdio> #include <cstring> using namespace std; const i

[POJ3662] Telephone Lines

题意:n个点,m条边,有k次机会免费走过一条边,最小化1-n的最大边权 题解:spfa+二分 这题好像克我......,一开始写了个二分+spfa在洛谷上A了,poj上莫名wa掉 然后写另一种方法,读入读成点数了,一直没发现,wa了一万年...... 二分最大边权 check把大于mid的边赋为1,小于等于mid的赋为0,spfa即可 #include<iostream> #include<cstdio> #include<cstdlib> #include<cs

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的电话杆已经接入了全国的电话网络,整个

POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 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

poj 3662 Telephone Lines(最短路+二分)

Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 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