zoukankan      html  css  js  c++  java
  • poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

    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

     
    二分枚举每条边的长度l,然后以这条边的长度l为界限重新建立地图,大于l的赋值为1,小于的赋值为0,然后跑一个dijkstra求出大于l的有多少个,然后继续二分枚举。
    注意一开始边要排序!!!
    注意一开始要先建立个以0为界限的图,判断是否可以到达,或者0的特判!!!
     
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<stdlib.h>
      6 #include<cmath>
      7 using namespace std;
      8 #define inf 1<<30
      9 #define N 10006
     10 #define M 1006
     11 int n,p,k;
     12 struct Node{
     13     int u,v,l;
     14 }node[N];
     15 int mp[M][M];
     16 void build_map(int length){//建图
     17     for(int i=0;i<=n;i++){
     18         for(int j=0;j<=n;j++){
     19             mp[i][j]=inf;
     20         }
     21     }
     22     for(int i=0;i<p;i++){
     23         int a=node[i].u;
     24         int b=node[i].v;
     25         int c=node[i].l;
     26         if(c>length){
     27             mp[a][b]=mp[b][a]=1;
     28         }
     29         else{
     30             mp[a][b]=mp[b][a]=0;
     31         }
     32     }
     33 
     34 }
     35 
     36 int dijkstra(int st){//dijkstra求从1到n的距离
     37     int vis[M];
     38     int dis[M];
     39     for(int i=0;i<=n;i++){
     40         vis[i]=0;
     41         dis[i]=inf;
     42     }
     43     vis[st]=1;
     44     dis[st]=0;
     45     int x=n;
     46     while(x--){
     47         for(int i=1;i<=n;i++){
     48             if(dis[st]+mp[st][i]<dis[i]){
     49                 dis[i]=dis[st]+mp[st][i];
     50             }
     51         }
     52         int minn=inf;
     53         for(int i=1;i<=n;i++){
     54             if(!vis[i] && dis[i]<minn){
     55                 minn=dis[i];
     56                 st=i;
     57             }
     58         }
     59         vis[st]=1;
     60     }
     61     return dis[n];
     62 }
     63 bool solve(int mid){//二分搜索的判断函数
     64     build_map(node[mid].l);
     65     int ans=dijkstra(1);
     66     if(ans<=k) return true;
     67     return false;
     68 }
     69 void go(){//二分搜索
     70     int low=0;
     71     int high=p;
     72     while(low<high){
     73         int mid=(low+high)>>1;
     74         if(solve(mid)){
     75             high=mid;
     76         }
     77         else{
     78             low=mid+1;
     79         }
     80     }
     81     printf("%d
    ",node[low].l);
     82 }
     83 bool cmp(Node a,Node b){//排序函数!!!
     84     return a.l<b.l;
     85 }
     86 int main()
     87 {
     88     while(scanf("%d%d%d",&n,&p,&k)==3){
     89         for(int i=0;i<p;i++){
     90             scanf("%d%d%d",&node[i].u,&node[i].v,&node[i].l);
     91         }
     92         sort(node,node+p,cmp);//二分搜索一定要先排序!!!???
     93         build_map(0);//先以0为界限建立图
     94         int ans=dijkstra(1);
     95         //printf("%d
    ",ans);
     96         if(ans==inf){//如果不能到达,输出-1
     97             printf("-1
    ");
     98         }
     99         else{
    100             if(ans<=k){//如果一开始就不用付出,则输出0
    101                 printf("0
    ");
    102             }
    103             else{
    104                 go();//二分搜索
    105             }
    106         }
    107     }
    108     return 0;
    109 }
    View Code
  • 相关阅读:
    vue 子组件像父组件传递数据
    SQL Query XML column.   SQL 查询 xml 字段
    最方便的批处理延时方法
    Automation testing framework for RFT execution with STAF+STAX . [Session1]
    Disable Windows server 2003 Security Warning.
    Perl初级教程 (5) 遍历文件夹内指定扩展名文件,查找匹配关键字的输出。
    Perl 基于 Windows 环境 搭建
    Perl Scalar
    Package you execution files with Iexpress.exe
    SQLServer2005 remove log file.
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4783268.html
Copyright © 2011-2022 走看看