码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3662 Telephone Lines(最短路+二分)

时间:2017-05-30 19:21:57      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:void   queue   多少   nbsp   data   silver   arch   stream   rate   

Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6973   Accepted: 2554

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

Source

这题意有毒  题意是
n个农场  每个农场通电话需要w[i]长度的电话线  其中k根电话线免费 
费用是这样定义的  除去这k根免费的电话线 剩下的最长的电话线作为花费的费用 问求最少花费的费用是多少 就是求第k+1大线的最小
我们二分答案  x;
用dijkstra算法  假如 边值>x  边值=1  否则等于0
所以我们只有统计这这条路径大于x的个数  如果大于k个 证明x还可以变大
否则x变小
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cctype>
  5 #include<cmath>
  6 #include<queue>
  7 #include<cstring>
  8 #include<map>
  9 #include<stack>
 10 #include<set>
 11 #include<vector>
 12 #include<algorithm>
 13 #include<string.h>
 14 typedef long long ll;
 15 typedef unsigned long long LL;
 16 using namespace std;
 17 const int INF=0x3f3f3f3f;
 18 const double eps=0.0000000001;
 19 const int N=100000+10;
 20 struct node{
 21     int to,next,w;
 22     bool operator<(const node &a)const{
 23         return w>a.w;
 24     }
 25 }edge[N*2];
 26 int t;
 27 int head[N];
 28 int vis[N],dis[N];
 29 int n;
 30 void init(){
 31     memset(vis,0,sizeof(vis));
 32     memset(head,-1,sizeof(head));
 33     t=0;
 34     for(int i=0;i<N;i++)dis[i]=INF;
 35 }
 36 void add(int u,int v,int w){
 37     edge[t].to=v;
 38     edge[t].w=w;
 39     edge[t].next=head[u];
 40     head[u]=t++;
 41 }
 42 int Dijkstra(int x,int y){
 43     memset(dis,INF,sizeof(dis));
 44     dis[x]=0;
 45     node t1,t2;
 46     t1.to=x;
 47     priority_queue<node>q;
 48     q.push(t1);
 49     memset(vis,0,sizeof(vis));
 50     while(!q.empty()){
 51         //cout<<2<<endl;
 52         t1=q.top();
 53         q.pop();
 54         int u=t1.to;
 55         if(vis[u]==1)continue;
 56         vis[u]=1;
 57         for(int i=head[u];i!=-1;i=edge[i].next){
 58             int v=edge[i].to;
 59             int tt=0;
 60             if(edge[i].w>=y)tt=1;
 61             if(vis[v]==0&&dis[v]>dis[u]+tt){
 62                 dis[v]=dis[u]+tt;
 63                 t2.to=v;
 64                 t2.w=dis[v];
 65                 q.push(t2);
 66             }
 67         }
 68     }
 69     return dis[n];
 70 }
 71 int main(){
 72     int k,p;
 73     while(scanf("%d%d%d",&n,&p,&k)!=EOF){
 74         init();
 75         int maxx=0;
 76         for(int i=0;i<p;i++){
 77             int u,v,w;
 78             scanf("%d%d%d",&u,&v,&w);
 79             add(u,v,w);
 80             add(v,u,w);
 81             maxx=max(maxx,w);
 82         }
 83         int low=0;
 84         int high=maxx;
 85         int ans=0;
 86         while(low<=high){
 87             int mid=(low+high)>>1;
 88             //cout<<Dijkstra(1,mid)<<endl;
 89             if(Dijkstra(1,mid)<=k){
 90                 high=mid-1;
 91             }
 92             else{
 93                 ans=mid;
 94                 low=mid+1;
 95             }
 96 
 97         }
 98         if(low>maxx){cout<<-1<<endl;continue;}
 99         cout<<ans<<endl;
100         //for(int i=1;i<=n;i++)cout<<dis[i]<<endl;
101     }
102 }

 

 

poj 3662 Telephone Lines(最短路+二分)

标签:void   queue   多少   nbsp   data   silver   arch   stream   rate   

原文地址:http://www.cnblogs.com/Aa1039510121/p/6920913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!