标签:nat require max 数字 UI nbsp sharp property main
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.
多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。
第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。
电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.
请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?
输入文件的第一行包含三个数字n,p,k;
第二行到第p+1行,每行分别都为三个整数ai,bi,li。
输出格式:一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.
Solution
思路其实不难,先二分一个答案M,那么大于M的边长变为1,小于等于的变为0,然后用最短路算出1到N的距离,如果大于K(有
超过K的路大于M)那么就不行,否则可以。直至找到答案。
#include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int cnt,head[1005],n,k,p; long long dis[20005]; long long que[20005],hea,tail; bool cl[20005]; struct road { int to,next,w,w2; }e[20005]; void add(int x,int y,int z) { cnt++; e[cnt].to=y; e[cnt].w=z; e[cnt].next=head[x]; head[x]=cnt; } void SPFA(int x) { memset(cl,0,sizeof(cl)); for(int i=1;i<=n;i++) dis[i]=2147483647; hea=0; tail=1; que[tail]=x; dis[x]=0; cl[x]=1; do { hea++; int u=que[hea]; cl[u]=0; for(int i=head[u];i!=0;i=e[i].next) { int v=e[i].to; if(e[i].w2+dis[u]<dis[v]) { dis[v]=dis[u]+e[i].w2; if(!cl[v]) { tail++; que[tail]=v; cl[v]=1; } } } }while(hea<tail); return ; } bool check(int m) { for(int i=1;i<=cnt;i++) if(e[i].w<=m) e[i].w2=0; else e[i].w2=1; SPFA(1); // for(int i=1;i<=n;i++) // cout<<dis[i]<<‘ ‘; // cout<<endl; if(dis[n]>k) return 0; else return 1; } int main() { int l,r,m; l=0; r=0; cin>>n>>p>>k; for(int i=1;i<=p;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); r=max(r,z); add(x,y,z); add(y,x,z); } int ans; bool t=0; while(l<=r) { m=(l+r)/2; if(!check(m)) l=m+1; else {ans=m; t=1;r=m-1;} } if(!t) cout<<-1; else cout<<ans; return 0; }
标签:nat require max 数字 UI nbsp sharp property main
原文地址:http://www.cnblogs.com/Le-mon/p/7739273.html