您的位置:首页 > 其它

USACO 08 JAN 电话线Telephone Lines(二分法在图论题中的应用)

2017-07-19 11:40 531 查看


电话线Telephone Lines

USACO08JAN(From:Luogu P1948)

题目描述

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

思路

说实话这个题我上来是往SPFA求两边最短路这个方向上考虑的,先按每个点权值为1来算最少电线数,以此来检验是否能够<k,同时还能检验此图联通,如果>k,再按正常权值过一遍SPFA求第k+1长的电线尽量短的情况,然后发现这种方法自己不会解(事实上刚开始正解上的做法我也没见过),然后就看题解。

于是乎比较惊讶的发现此题可以用二分来求。

具体思路就是:

二分对象是需要消耗的价值,然后我们要检验吗,检验的时候就是要检验是否能有一个数d,使得到达n点路径的电线中>d的条数的最小值f
<=k,如果满足就说明最优解比当前值d要小,就让d变成right,反之就是d不能满足题意,要满足题意需要让d大一些,就让d+1变成left。

最后求出最优解的时候想着在检验一遍left或者right,看看它是否符合题意,如果不合题意就输出-1。

#include<iostream>
#include<deque>
#include<cstring>
using namespace std;
int i,j,k,m,n,st,en,w;
int le,ri,mid;

struct node
{
int y;
int v;
struct node *next;
}a[20005];
int head[1005];
int f[1005],ans;
bool b[1005];

int r()
{
int ans=0,f=0;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
ans*=10;
ans+=ch-'0';
ch=getchar();
}
return ans;
}

int spfa(int x)
{
int xx=1;
deque<int> q;
memset(f,0x7f7f7f,sizeof(f));
f[xx]=0;
q.push_front(xx);
b[xx]=1;
struct node *p;
while(!q.empty())
{
p=&a[head[q.front()]];
xx=q.front();
q.pop_front();
b[xx]=0;
int yy;
while(p!=NULL)
{
yy=p->y;
if(f[yy]>((p->v)>x)+f[xx])
{
f[yy]=((p->v)>x)+f[xx];
if(!b[yy])
{
b[yy]=1;
q.push_back(yy);
}
}
p=p->next;
}}
return f
<=k;
}

int erfen()
{
while(le<ri)
{
mid=(le+ri)/2;
if(spfa(mid))
{
ri=mid;
}
else
{
le=mid+1;
}
}
if(spfa(le))
cout<<le;
else
cout<<-1;
}

int main()
{
n=r(),m=r(),k=r();
for(i=1;i<=m;i++)
{
st=r(),en=r(),w=r();
a[2*i-1].y=en;
a[2*i-1].v=w;
a[2*i-1].next=&a[head[st]];
head[st]=2*i-1;
a[2*i].y=st;
a[2*i].v=w;
a[2*i].next=&a[head[en]];
head[en]=2*i;
ri=max(ri,w);
}
erfen();
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: