zoukankan      html  css  js  c++  java
  • POJ 3662 (二分+SPFA

    Telephone Lines
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8856   Accepted: 3211

    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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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: NP, and K
    * Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

    题意:求一条路径从1到n使第k+1大的边最小。
    思路:二分第k+1大的路径长度mid,大于mid的边标记为1,否则标记为0,跑一遍SPFA,当d[n]<=k就为满足条件,取最小的mid。
    代码:
     1 #include"bits/stdc++.h"
     2 #define db double
     3 #define ll long long
     4 #define vl vector<ll>
     5 #define ci(x) scanf("%d",&x)
     6 #define cd(x) scanf("%lf",&x)
     7 #define cl(x) scanf("%lld",&x)
     8 #define pi(x) printf("%d
    ",x)
     9 #define pd(x) printf("%f
    ",x)
    10 #define pl(x) printf("%lld
    ",x)
    11 #define rep(i, n) for(int i=0;i<n;i++)
    12 using namespace std;
    13 const int N   = 1e6 + 5;
    14 const int mod = 1e9 + 7;
    15 const int MOD = 998244353;
    16 const db  PI  = acos(-1.0);
    17 const db  eps = 1e-10;
    18 int R()
    19 {
    20     int f=1,x=0;
    21     char e=getchar();
    22     while(e<'0'||e>'9'){if(e=='-')f=-1;e=getchar();}
    23     while(e>='0'&&e<='9'){x=x*10+e-'0';e=getchar();}
    24     return f*x;
    25 }
    26 
    27 const int inf=1128481603;
    28 int n,m,k;
    29 struct P{int v,dis,nxt;}E[50005];
    30 int head[N],tot;
    31 int d[2005],vis[2005];//数组开太大会T
    32 int l=0,r=1e6+10,mid;
    33 
    34 void add(int u,int v,int dis)
    35 {
    36     E[++tot].nxt=head[u];
    37     E[tot].v=v; E[tot].dis=dis;
    38     head[u]=tot;
    39 }
    40 
    41 void SPFA()
    42 {
    43     memset(d,67,sizeof(d)); d[1]=0;
    44     queue<int> q; q.push(1);
    45     memset(vis,0,sizeof(vis));
    46     while(!q.empty())
    47     {
    48         int u=q.front();
    49         q.pop(); vis[u]=0;
    50         for(int i=head[u];i;i=E[i].nxt)
    51         {
    52             int v=E[i].v,dis=(E[i].dis>mid);
    53             if(d[v]>d[u]+dis)
    54             {
    55                 d[v]=d[u]+dis;
    56                 if(!vis[v])q.push(v),vis[v]=1;
    57             }
    58         }
    59     }
    60 }
    61 
    62 int main()
    63 {
    64     n=R();m=R();k=R();
    65     for(int i=1;i<=m;++i)
    66     {
    67         int u=R(),v=R(),dis=R();
    68         add(u,v,dis);add(v,u,dis);
    69     }
    70     int ans=-1;
    71     while(l<=r)
    72     {
    73         mid=(l+r)>>1;
    74         SPFA();
    75         if(d[n]==inf){ printf("-1"); return 0;}
    76         if(d[n]<=k) ans=mid,r=mid-1;
    77         else l=mid+1;
    78     }
    79     printf("%d",ans);
    80     return 0;
    81 }
  • 相关阅读:
    mysql8.0授权远程登录
    vbox 网络配置
    dnmp安装
    Unix/Linux环境C编程入门教程(7) OPENBSDCCPP开发环境搭建
    Unix/Linux环境C编程入门教程(6) 安装Fedora C/C++开发环境
    Unix/Linux环境C编程入门教程(5) Red Hat Enterprise Linux(RHEL)环境搭建
    Unix/Linux环境C编程入门教程(4) Debian Linux环境搭建
    C语言入门(8)——形参与实参
    C语言入门(7)——自定义函数
    C语言入门(6)——C语言常用数学函数
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/9502436.html
Copyright © 2011-2022 走看看