POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

简介: Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 Description Farmer John wants to set up a telephone line at his farm.

Telephone Lines

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7214   Accepted: 2638

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个电线杆,有P对电线杆是可以连接的,用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电线杆N能相互通信,并且电线公司提出K条电线是可以免费使用的,当使用电线的数量超过K条,超出的电线要收费,收的总费用为去掉免费使用的K条电线之后最长的那条电线的长度。现在需要尽可能的减少费用,问最少费用是多少

解题思路:最短路+二分,二分第k+1条长的长度,然后按照二分的值用0,1处理整个图:将比二分值大的边都置为1,将比二分值小的边都置位0,然后进行找1到n的最短路。不过若1到n的边数小于等于k,答案为0

下面给出AC代码:

 

  1 #include <iostream>
  2 #include<algorithm>
  3 #include<queue>
  4 #include<stack>
  5 #include<cmath>
  6 #include<string.h>
  7 #include<stdio.h>
  8 #include<stdlib.h>
  9 #include <set>
 10 #include <map>
 11 using namespace std;
 12 inline int read()
 13 {
 14     int x=0,f=1;
 15     char ch=getchar();
 16     while(ch<'0'||ch>'9')
 17     {
 18         if(ch=='-')
 19             f=-1;
 20         ch=getchar();
 21     }
 22     while(ch>='0'&&ch<='9')
 23     {
 24         x=x*10+ch-'0';
 25         ch=getchar();
 26     }
 27     return x*f;
 28 }
 29 inline void write(int x)
 30 {
 31     if(x<0)
 32     {
 33         putchar('-');
 34         x=-x;
 35     }
 36     if(x>9)
 37         write(x/10);
 38     putchar(x%10+'0');
 39 }
 40 struct Node
 41 {
 42     int id,dis;
 43 };
 44 bool operator<(const Node &a,const Node &b)
 45 {
 46     return a.dis>b.dis;
 47 }
 48 typedef struct
 49 {
 50     int v,next,cost;
 51 }Edge;
 52 Edge e[50050];
 53 int head[1010];
 54 int d[1010];
 55 int n,p,k;
 56 int cnt;
 57 inline bool BFS()
 58 {
 59     int vis[1010];
 60     memset(vis,false,sizeof(vis));
 61     queue<int>Q;
 62     Q.push(1);
 63     vis[1]=1;
 64     while(!Q.empty())
 65     {
 66         int u=Q.front();
 67         Q.pop();
 68         for(int i=head[u];i!=-1;i=e[i].next)
 69         {
 70             int v=e[i].v;
 71             if(vis[v])
 72                 continue;
 73             vis[v]=1;
 74             if(v==n)
 75                 return 1;
 76             Q.push(v);
 77         }
 78     }
 79     return 0;
 80 }
 81 inline int Dijkstra(int ans)
 82 {
 83     priority_queue<Node>q;
 84     Node temp,pp;
 85     bool vis[1010];
 86     for(int i=1;i<=n;i++)
 87     {
 88         d[i]=1e+8;
 89         vis[i]=0;
 90     }
 91     d[1]=0;
 92     temp.id=1;
 93     temp.dis=0;
 94     q.push(temp);
 95     while(!q.empty())
 96     {
 97         pp=q.top();
 98         q.pop();
 99         int u=pp.id;
100         if(vis[u])
101             continue;
102         vis[u]=1;
103         for(int i=head[u];i!=-1;i=e[i].next)
104         {
105             int v=e[i].v;
106             int t=e[i].cost>ans?true:false;
107             if(!vis[v])
108             {
109                 if(d[v]>d[u]+t)
110                 {
111                     d[v]=d[u]+t;
112                     pp.id=v;
113                     pp.dis=d[v];
114                     q.push(pp);
115                 }
116             }
117         }
118     }
119     return d[n];
120 }
121 int main()
122 {
123     int fr,to,cost;
124     while(scanf("%d%d%d",&n,&p,&k)!=EOF)
125     {
126         cnt=1;
127         memset(e,0,sizeof(e));
128         memset(head,-1,sizeof(head));
129         for(int i=0;i<p;i++)
130         {
131             fr=read();
132             to=read();
133             cost=read();
134             e[cnt].v=to;
135             e[cnt].next=head[fr];
136             e[cnt].cost=cost;
137             head[fr]=cnt;
138             cnt++;
139             e[cnt].v=fr;
140             e[cnt].next=head[to];
141             e[cnt].cost=cost;
142             head[to]=cnt;
143             cnt++;
144         }
145         if(!BFS())
146         {
147             puts("-1");
148             continue;
149         }
150         int l=0,r=1000010,mid;
151         while(l<=r)
152         {
153             mid=(l+r)/2;
154             if(Dijkstra(mid)<=k)
155                 r=mid-1;
156             else
157                 l=mid+1;
158         }
159         write(r+1);
160         printf("\n");
161     }
162     return 0;
163 }

 

 

 

目录
相关文章
|
算法
【学会动态规划】最小路径和(9)
【学会动态规划】最小路径和(9)
41 0
|
机器学习/深度学习 存储 算法
回溯法求解N皇后问题
回溯法求解N皇后问题
149 0
HDU7018.Banzhuan(计算几何+贪心)
HDU7018.Banzhuan(计算几何+贪心)
104 0
HDU7018.Banzhuan(计算几何+贪心)
|
机器学习/深度学习 算法 程序员
【算法合集】深搜广搜Prim与Kruskal
广度优先搜索(BFS)又叫广搜,它像一个有远见的人,它是一层一层来实现搜索的,也挺像下楼梯的。 思路: 1.先初始化队列 q; 2.从起点开始访问,并且改变他的状态为已经访问; 3.如果他的队列非空,取出首个元素,将它弹出! 4.如果u == 目标状态,然后对所以与 u 邻近的点进入队列; 5.标记它已经被访问!...............
172 1
【算法合集】深搜广搜Prim与Kruskal
|
Go
[Nowcoder / POJ2728] 最优比率生成树 | 二分 + prim
有n个点,其中,每个点给出位置坐标( x , y ) 以及高度z ,两点之间的距离为两点之间的欧几里得距离 两点之间建立一条路的代价为两点之间的高度差,问将n 个点联通的情况下,求出最大的cost/dis
128 0
HDU-1874,畅通工程续(Floyd最短路)
HDU-1874,畅通工程续(Floyd最短路)