题目:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8178 | Accepted: 2948 |
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
题意:有n个点,p条双向边,任务是要使1-n联通。在1走到n的路径上,可以任意选择k条边免费,剩下的边中最大的那一条的长度就是要支付的费用,求最小需要支付的费用。
思路:在1走到n的路径中上,我们肯定要使权值最大的k条边免费,这样才能支付最小的费用(第1大到第k大的边免费第k+1大的边权就是支付的费用)。但是我们无法知道这条路径上的边权值都是些什么,也就无法直接找出前k大的所有边。于是我们就可以二分猜测一下这个第k+1大的边权值是什么,再计算路径上比这个边大的边数,并比较它与k的关系(如果值比k多则说明猜测的第k+1大边取小了,反之),从而最终找到解。
在假定完第k+1打的边权之后,我们需要怎样才能计算出路径上比这个边大的边有多少条呢?最短路可以巧妙地计算。令比假定值大的边权值为1,否则为0。那么跑完最短路之后,由于小于假定值的边权值均为0,1-n最短路的值就是路径上所有比假定值大的边的总数。
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
#define eps 1.0E-8
// #define Reast1nPeacetypedef long long ll;using namespace std;const int INF = 0x3f3f3f3f;
const int maxn = 20010;
int n,p,k;
int cnt;struct edge{int to;int w;int c;int nxt;
}G[maxn]; int head[1010];void Add(int a , int b , int l){G[cnt].to = b , G[cnt].w = l;G[cnt].nxt = head[a];head[a] = cnt;cnt++;
}int len[1010];
int vis[1010];
struct node{int id,len;node(int id,int len){this->id = id, this->len = len;}friend bool operator <(node a , node b){return a.len > b.len;}
};int dijkstra(){memset(len,INF,sizeof(len));memset(vis,0,sizeof(vis));priority_queue<node> q;q.push(node(1,0)) , len[1] = 0;while(!q.empty()){node t = q.top() ; q.pop();int now = t.id; int L = t.len;vis[t.id] = 1;for(int i = head[now] ; i!=0 ; i = G[i].nxt){int nxt = G[i].to;if(vis[nxt]) continue;if(len[nxt] > L+G[i].c){len[nxt] = L+G[i].c;q.push(node(nxt,len[nxt]));}} } return len[n];
}bool judge(int x){for(int i = 1 ; i<=n ; i++){for(int j = head[i] ; j!=0 ; j = G[j].nxt){if(G[j].w <= x) G[j].c = 0;else G[j].c = 1;}}int nums = dijkstra();if(nums <= k) return true;else return false;
}int main(){
#ifdef Reast1nPeacefreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);
#endifios::sync_with_stdio(false);cin>>n>>p>>k;int a,b,ll;cnt = 1;for(int i = 1 ; i<=p ; i++){cin>>a>>b>>ll;Add(a,b,ll);Add(b,a,ll);}int l = 0 ; int r = 1000000;int ans = -1;while(l<=r){int mid = (l+r)>>1;if(judge(mid)){ans = mid;r = mid-1;}else{l = mid+1;}}cout<<ans<<endl;return 0;
}