Telephone Linse(POJ-3662)
Problem 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條邊,可以任選k條邊免費?,F在要使1到n連通,從1到n的路徑上,在不免費的邊中最大的長度的費用即支付費用,求最小費用。
思路
由于要求最小費用,所以讓公司提供k根比較長的,剩下的由個人提供支付費用,然后從中挑出最長的L,也是說只要是長度比L長的且在這條路上用到的電纜,那么就應該由電信公司提供。?
對于每個方案,假設個人提供的最長電纜長度是L,從1到N找出一條路來,使這條路上的長度比L大的電纜的個數不超過k即可,然后求最短路。
對于所有方案,不斷二分查找,使L盡可能的小。?
Source Program
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 1001 #define MOD 2520 #define E 1e-12 using namespace std; int n,p,k; int x[N*10],y[N*10],w[N*10]; int head[N],tail;//添加方案時的指針 queue<int>Queue;//隊列 int dis[N];//存儲最短路路徑 bool vis[N];//用于最短路路徑判斷 struct Node{int w;//節點的長度int pre;//結點的前驅int next;//結點的后繼 }a[20005]; void addedge(int mid) {tail=0;memset(head,-1,sizeof(head));for(int i=0;i<p;i++){/*添加從x到y*/if(w[i]>mid)a[tail].w=1;elsea[tail].w=0;a[tail].pre=y[i];a[tail].next=head[x[i]];head[x[i]]=tail;tail++;/*添加從y到x*/if(w[i]>mid)a[tail].w=1;elsea[tail].w=0;a[tail].pre=x[i];a[tail].next=head[y[i]];head[y[i]]=tail;tail++;} } int SPFA() {memset(vis,0,sizeof(vis));memset(dis,INF,sizeof(dis));dis[1]=0;vis[1]=1;Queue.push(1);while(!Queue.empty()){int v=Queue.front();vis[v]=0;Queue.pop();for(int i=head[v];i!=-1;i=a[i].next){Node temp=a[i];if(dis[temp.pre]>dis[v]+temp.w){dis[temp.pre]=dis[v]+temp.w;if(vis[temp.pre]==0){Queue.push(temp.pre);vis[temp.pre]=1;}}}}return dis[n]; }int main() {while(scanf("%d%d%d",&n,&p,&k)!=EOF){int maxx=-INF;for(int i=0;i<p;i++){scanf("%d%d%d",&x[i],&y[i],&w[i]);maxx=max(maxx,w[i]);//尋找最大長度}if(n==1)printf("0\n");else//二分查找最佳答案{int left=-1,right=maxx,mid;while(right-left>1){mid=(right+left)/2;addedge(mid);//將當前mid的值視為一種方案添加到隊列中int minn=SPFA();if(minn==INF){right=-1;break;}else if(minn<=k)right=mid;elseleft=mid;}printf("%d\n",right);}}return 0; }?
總結
以上是生活随笔為你收集整理的Telephone Linse(POJ-3662)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 石子合并问题--直线版(Hrbust-1
- 下一篇: 多米诺骨牌(洛谷-P1282)