POJ3662Telephone Lines(最短路+二分)

传送门

题目大意:n个点p条边,每条边有权值,让1和n点联通,可以将联通1--n的边选k条免费,

求剩下边权的最大值。

题解:二分一个答案x,大于x的边权设为1,小于等于x的边权设为0,跑最短路。

若从1到n的最短路dis[n]<=k,则可以通过免费k条边,答案为x。

代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define N 1009
#define M 10009
using namespace std;

int n,p,k;

int sumedge;

int head[N],dis[N],vis[N];

queue<int>q;

struct Edge
{
    int x,y,z,nxt;
    Edge(int x=0,int y=0,int z=0,int nxt=0):
        x(x),y(y),z(z),nxt(nxt){}
}edge[M<<1];

void add(int x,int y,int z)
{
    edge[++sumedge]=Edge(x,y,z,head[x]);
    head[x]=sumedge;
}

bool ok(int tp)
{
    while(!q.empty()) q.pop();
    memset(dis,0x3f,sizeof(dis));
    q.push(1);vis[1]=true;dis[1]=0;
    while(!q.empty())
    {
        int now=q.front();q.pop(); vis[now]=false;
        for(int i=head[now];i;i=edge[i].nxt)
        {
            int v=edge[i].y;
            if(dis[v]>dis[now]+(edge[i].z<=tp?0:1))
            {
                dis[v]=dis[now]+(edge[i].z<=tp?0:1);
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
    return dis[n]<=k;
}

int main()
{
    scanf("%d%d%d",&n,&p,&k);
    int l=0,r=0,ans=-1;
    for(int i=1;i<=p;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);add(y,x,z);
        r=max(r,z);
    }
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(ok(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    cout<<ans<<endl;
    return 0;
} 

原文地址:https://www.cnblogs.com/zzyh/p/11990749.html

时间: 2024-10-11 16:06:59

POJ3662Telephone Lines(最短路+二分)的相关文章

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

HDU1839_Delay Constrained Maximum Capacity Path(最短路+二分)

解题报告 http://blog.csdn.net/juncoder/article/details/38349019 题目传送门 题意: 有N个点,点1为珍贵矿物的采矿区, 点N为加工厂,有M条双向连通的边连接这些点.走每条边的运输容量为C,运送时间为D. 他们要选择一条从1到N的路径运输, 这条路径的运输总时间要在T之内,在这个前提之下,要让这条路径的运输容量尽可能地大. 一条路径的运输容量取决与这条路径中的运输容量最小的那条边. 思路: 二分容量建图,spfa判时间是否符合条件 #incl

hdu2962 Trucking (最短路+二分查找)

Problem Description A certain local trucking company would like to transport some goods on a cargo truck from one place to another. It is desirable to transport as much goods as possible each trip. Unfortunately, one cannot always use the roads in th

CSU 1307 最短路+二分

题目大意: 帮忙找到一条a到b的最短路,前提是要保证路上经过的站点的最大距离尽可能短 这道题居然要用到二分...完全没去想过,现在想想求最大距离的最小值确实是... 这里不断二分出值代入spfa()或者dijkstla()中计算a到b的最短距离,每次都保证只经过边小于mid值的路径 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 #incl

poj3662Telephone Lines——二分+最短路

题目:http://poj.org/problem?id=3662 二分答案找出符合条件的最小长度: 假设了每个长度后,以这个为标准对每条边赋值,0为小于等于,1为大于,然后按这个值来跑最短路,在看看能否使用不超过k根长电线: 注意不能到达要输出-1! 不知为何l从0开始就A了,从最短的电线开始就是WA,可怖的细节: 总之,0和1这个技巧很美,打破了最短路的常规思路. 代码如下: #include<iostream> #include<cstdio> #include<cst

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

[Usaco2007 Jan]Telephone Lines架设电话线[二分答案+最短路思想]

Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废弃的电话线杆,任意两根电话线杆间都没有电话线相连.一共P(1 <= P <= 10,000)对电话线杆间可以拉电话线,其余的那些由于隔得太远而无法被连接. 第i对电话线杆的两个端点分别为A_i.B_i,它们间的距离为 L_i (1 <= L_i

hdu 2962 Trucking 最短路+二分。。Dijkstra+SPFA两种算法实现。

Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1966    Accepted Submission(s): 680 Problem Description A certain local trucking company would like to transport some goods on a cargo

hdu2962(最短路+二分)

题意:在最大的高度下面求最短路,由于题目给出限高,所以我们只需要二分高度然后用SPFA #include <iostream> #include <string.h> #include <queue> #include <vector> #include <utility> #include <cstdio> #include <cstring> #include <algorithm> using names