POJ_3662_Telephone_Lines_(二分+最短路)

描述


http://poj.org/problem?id=3662

给一张图,要将1与n连起来.可以有k条边免费,其他边自费,付费的值为所有自费边中最大的值.求最小付费.

Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5932   Accepted: 2152

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

分析


可以假定一个付费值m,所有<=m的都自己付费,>m的免费,然后使>m的边数a尽可能小,看是否可以使得a<=k.这里用二分即可.

统计a的最小值时,我们考虑:边分为免费边和自费边,要让免费边数量尽可能小,就把免费边赋值为1,自费边赋值为0,跑最短路,最后d[n]就是最少经过的免费边数量a.

另外,二分的标准如果是0~maxl的话就太大了,我们可以把每一条边的值存在f数组中,然后二分f[0]~f[p],这样会小很多.

注意:

1.如果自费最大费用(不用免费的了),还是不能成功,就输出-1.

Dijkstra:

 #include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=,maxp=,INF=<<;
struct node
{
int to,w;
node(){}
node(int a,int b) : to(a),w(b){}
bool operator < (const node &a) const { return a.w>w; }
};
vector <node> g[maxn];
int n,p,k;
int d[maxn],f[maxp]; bool Dijkstra(int m)
{
for(int i=;i<=n;i++) d[i]=INF;
d[]=;
priority_queue <node> q;
q.push(node(,));
while(!q.empty())
{
int x=q.top().to;q.pop();
for(int i=;i<g[x].size();i++)
{
int y=g[x][i].to,dxy=g[x][i].w;
dxy=dxy>m ? : ;
if(d[y]>d[x]+dxy)
{
d[y]=d[x]+dxy;
q.push(node(y,d[y]));
}
}
}
return (d[n]<=k);
} void solve()
{
if(!Dijkstra(f[p])) { printf("-1\n"); return; }
int l=,r=p;
while(l<r)
{
int m=l+(r-l)/;
if(Dijkstra(f[m])) r=m;
else l=m+;
}
printf("%d\n",f[l]);
} void init()
{
scanf("%d%d%d",&n,&p,&k);
for(int i=;i<=p;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(node(b,c));
g[b].push_back(node(a,c));
f[i]=c;
}
sort(f+,f++p);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("phone.in","r",stdin);
freopen("phone.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}

Spfa:

 #include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=,maxp=,INF=<<;
struct node
{
int to,w;
node(){}
node(int a,int b) : to(a),w(b){}
};
vector <node> g[maxn];
int n,p,k;
int d[maxn],f[maxp];
bool vis[maxn]; bool Spfa(int m)
{
for(int i=;i<=n;i++) { d[i]=INF; vis[i]=false; }
d[]=; vis[]=true;
queue <int> q;
q.push();
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=false;
for(int i=;i<g[x].size();i++)
{
int y=g[x][i].to,dxy=g[x][i].w;
dxy=dxy>m ? : ;
if(d[y]>d[x]+dxy)
{
d[y]=d[x]+dxy;
if(!vis[y])
{
vis[y]=true;
q.push(y);
}
}
}
}
return (d[n]<=k);
} void solve()
{
if(!Spfa(f[p])) { printf("-1\n"); return; }
int l=,r=p;
while(l<r)
{
int m=l+(r-l)/;
if(Spfa(f[m])) r=m;
else l=m+;
}
printf("%d\n",f[l]);
} void init()
{
scanf("%d%d%d",&n,&p,&k);
for(int i=;i<=p;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(node(b,c));
g[b].push_back(node(a,c));
f[i]=c;
}
sort(f+,f++p);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("phone.in","r",stdin);
freopen("phone.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
上一篇:Swaps in Permutation


下一篇:CodeForces 691D:Swaps in Permutation(并查集)