Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8067 | Accepted: 2913 |
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
简而言之,就是让你从一张无相图上,找一条边,使边上第k+1大的边权值最小
最大值最小?二分?
思考,可以二分出一个ans,使得ans为椛的钱;
显然,我花更多的钱,可以使更多的边符合答案;
所以,题目就是是否有一个合法的边,使得ans成立?
如何判断ans是否成立?
可以把权值比ans大的边赋值为1,小于等于的赋值为0;
就是在一张01图上找是否有一条路,从1到n,使得椛费小于等于k;
然后双端队列bfs加剪枝,比垃圾spaf和什么sj....快很多;
详细见代码:
#include <cstdio> #include <algorithm> #include <deque> #include <queue> using namespace std; const int MAX=1010; struct data { int nxt,v,val; }edge[30010]; int cnt,alist[MAX]; int d[MAX]; bool vis[MAX]; inline void add(int u,int v,int val) { edge[++cnt].v=v;edge[cnt].val=val; edge[cnt].nxt=alist[u];alist[u]=cnt; } struct nod { int now,val; }; int n,p,k; bool check(int cos) { deque<nod> q; fill(d,d+n+1,-0x3f3f3f3f); //d数组记录到每个点所属剩余免费次数(记录的是k,就是还有几个免费可以用)
nod s;s.now=1;s.val=k;d[1]=1; q.push_back(s); while(!q.empty()) { nod p=q.front();q.pop_front(); int now=p.now;int res=p.val; if(now==n) //如果成立,则往小处二分 { return true; } for(int i=alist[now];i;i=edge[i].nxt) //遍历所有点 { int v=edge[i].v; int val=edge[i].val; if(val>cos && res-1>d[v] && res-1>=0) //若果椛费大于二分的答案并且 { // 我更新的剩余次数多,并且大于0 nod xx;xx.now=v;xx.val=res-1; q.push_back(xx); d[v]=res-1; } else if(val<=cos && res>d[v] && res>=0)//如果椛费小于等于,则反之 { nod xx;xx.now=v;xx.val=res; q.push_front(xx);d[v]=res; d[v]=res; } } } return false; ///不满足 } int r,l; int main() { // freopen("testdata.in","r",stdin); // freopen("qqq.out","w",stdout); scanf("%d%d%d",&n,&p,&k); for(int i=1;i<=p;++i) { int u,v,val; scanf("%d%d%d",&u,&v,&val); add(u,v,val);add(v,u,val); r=r>val?r:val; //二分上界就是最大的花费 } bool flag=false; //二分 while(l<r) { int mid=(l+r)>>1; if(check(mid)) r=mid,flag=true; else l=mid+1; } printf("%d",flag?r:-1); }