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 {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
Source
题目大意:有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);
}
}