Telephone LineTime limit (Common/Java): 3000 MS/3000 MS running memory limit: 65536 Kbyte Total submission: 38 tested: 13Description Farmer John planned to direct the telephone line to his farm, but the telecommunications company did not plan to provide him with free services. Therefore, FJ must pay a certain fee to the telecommunications company.
There are N (1 <= n <= 1,000) roots distributed around the farm of FJ .. N sequential number of discarded telephone lines, any two telephone lines are not connected to the telephone line. A total of P (1 <= P <= 10,000) can be used to pull telephone lines between telephone lines, and others cannot be connected due to the distance.
The two ends of the I-th telephone pole are A_ I and B _ I, respectively, and the distance between them is L_ I (1 <= L_ I <= 1,000,000 ). Ensure that each pair of {a_ I, B _ I} appears only once at most.
The telephone pole numbered 1 has been connected to the national telephone network, and all the telephone lines on the farm are connected to the telephone pole numbered n. That is to say, the task of Fj is only to find a path to connect telephone lines 1 and N. Other telephone lines do not have to be connected to the telephone network.
After negotiation, the telecommunications company eventually agreed to connect K (0 <= k <n) to the telephone pole specified by FJ for free. For other telephone lines, FJ will pay for them, equal to the length of the longest telephone line (each telephone line is associated with only one pair of telephone lines ). If the number of telephone lines to be connected does not exceed K pairs, the total cost of Fj is 0.
Calculate the minimum cost of Fj over the telephone line. Input * Row 1st: Three integers separated by spaces: N, P, and K * Row 2nd. p + 1: I + 1 Act 3 integers separated by spaces: a_ I, B _ I, L_ I Output * Row 1st: output an integer, which is the minimum expenditure of Fj on this project. If the task cannot be completed, output-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
Prompt Input description: A total of five discarded telephone lines. Telephone Line 1 cannot be directly connected to telephone lines 4 or 5. Telephone Line 5 cannot be directly connected to telephone lines 1 and 3. Telephone lines can be used between all other telephone lines. Telecom companies can connect one pair of telephone lines to FJ for free. Output description: FJ select the following link scheme: 1-> 3; 3-> 2; 2-> 5, the telephone lines required for the three telephone lines are 4, 3, and 9 respectively. FJ asked the telecommunications company to provide the telephone line with a length of 9, so the maximum length of the telephone line he needs to buy is 4. Question Source ZXY |
Question address: http: // 218.194.91.48/acmhome/problemdetail. do? & Method = showdetail & id = 1066
Method: bipartite enumeration + search.
Very good questions...
#include<iostream>#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;struct point{ int len,to,next;}p[20100];int f[1100];int dis[11000];int mark[1100];int n,k,pp,cnt;int que[400000];int bfs(int mx){ int s,t,v; int now,nx,num; que[s=t=0]=1; memset(mark,-1,sizeof(mark)); mark[1]=0; while(s<=t){ now=que[s++]; for(v=f[now];v!=-1;v=p[v].next){ num=mark[now]; nx=p[v].to; if(p[v].len>mx) num++; if(num>k) continue; if(mark[nx]!=-1) if(mark[nx]<=num) continue; if(nx==n) return 1; mark[nx]=num; que[++t]=nx; } } return 0;}int deal(){ int l=0,r=pp,mid; if(bfs(dis[r])==0) return -1; while(l<=r){ if(l==r) break; mid=(l+r)>>1; if(bfs(dis[mid])==1) r=mid; else l=mid+1; } return dis[l];}int main(){ int i,j; int x,y,len; while(~scanf("%d%d%d",&n,&pp,&k)){ memset(f,-1,sizeof(f)); dis[0]=0; for(i=1,cnt=0;i<=pp;i++){ scanf("%d%d%d",&x,&y,&len); p[cnt].to=y; p[cnt].len=len; p[cnt].next=f[x]; f[x]=cnt++; p[cnt].to=x; p[cnt].len=len; p[cnt].next=f[y]; f[y]=cnt++; dis[i]=len; } sort(dis,dis+pp+1); printf("%d\n",deal()); } return 0;}