您的位置:首页 > 其它

usaco 2008 January Telephone Lines 架设电话线 题解

2014-02-28 16:06 369 查看
Telephone Lines

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4541 Accepted: 1666
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

USACO 2008 January Silver

题目大意:有N个点(N<=1000),P条边(P<=10000)(ps:真稀疏啊!)你可以把其中的K条边的权值改为0。(K<=N) 最后要求出从起点(1)到终点(N)经过的边中的最大值的最小值。(ps:好绕啊!)

SYC大神邀请我做此题,并表示他用DP过了!(ORZ)

我想了一会,觉得可以用二分答案+验证。每次枚举经过的边最大值,效率是log(1000000)≈20,于是再来个SPFA就可以了(我表示:n^2的dijstra都能过啊!)。SPFA的过程要稍微做一点改变。我们设dis[i]的意义是“从起点开始最少把几条边变成0能到达i点”。一开始初始化成一个很大的数,且dis[1]=0。每次如果能更新dis[i],而且点i不在队列里,就把i更新到队列尾部。最后如果dis
<=k,就是满足要求的。

最近被USACO的16M内存吓怕了,连前向星的n^2矩阵都不敢写了,直接上边表。

最后还要在考虑-1的情况。如果出现-1,一定是无法从起点遍历到终点。我们可以在读入时记录边的最大值max,并在二分时枚举到max+1——这样,如果不能到达终点,一定返回max+1。

代码:

#include<stdio.h>
#include<cstring>
using namespace std;
const int Edge=20005,Vertex=1005;
struct arr{int l,r,s,next;}a[Edge];
int x[10*Vertex],dis[Vertex],begin[Vertex],end[Vertex];
bool flag[Vertex];
int n,m,k,xx,y,z,i,max,cnt,ans;
void make_up(int u,int v,int w)
{
cnt++;
a[cnt].l=u;a[cnt].r=v;a[cnt].s=w;a[cnt].next=-1;
if (begin[u]==0) {begin[u]=cnt;end[u]=cnt;}
else {a[end[u]].next=cnt;end[u]=cnt;}
}
bool check(int mid)
{
x[1]=1;int h=0,t=1;
memset(flag,0,sizeof(flag));
memset(dis,1,sizeof(dis));
flag[1]=true;dis[1]=0;
while (h<t)
{
int now=x[++h];
int p=begin[now];
while (a[p].l==now)
{
int go=a[p].r;int w=a[p].s>mid?1:0;
if (dis[now]+w<dis[go])
{
dis[go]=dis[now]+w;
if (!flag[go]){x[++t]=go;flag[go]=true;}
}
p=a[p].next;
}
flag[now]=false;
}
if (dis
>k) return false;return true;
}
int erfen(int l,int r)
{
if (l==r) return l;
int mid=(l+r)/2;
if (check(mid)) return erfen(l,mid);
return erfen(mid+1,r);
}

int main()
{
scanf("%ld%ld%ld",&n,&m,&k);
for (i=1;i<=m;i++)
{
scanf("%ld%ld%ld",&xx,&y,&z);
if (z>max) max=z;
make_up(xx,y,z);
make_up(y,xx,z);
}
ans=erfen(0,max+1);
if (ans==max+1) printf("-1");else printf("%ld",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  usaco月赛 图论 边表