POJ 3662

Telephone Lines

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..Nthat 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 ≤ KN) 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

题意:有N根电线杆,编号分别为1……n,然后有p对电线杆可以使用,这p电线杆需要一定长度的电线连接,电力公司会免费安装k条线路,剩下的需要农场自己安装,求剩下的电路中最长的电路长度是多少?

思路:暴力最短路根本是无法实现的,又因为是让求出这里面第k+1条最长线路,考虑二分---每次去枚举答案

这里还要有一个处理最短路的小技巧,就是套用最短路Dijistra的板子的时候dis数组改变的是大于当前枚举的答案mid个数,而不是将路径相加了,这一点很重要,然后看dis[n]值是否大于k,进而再二分

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const int MOD=1e6;
const int INF= 0x3f3f3f3f;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define init(a,b) memset(a,b,sizeof a)
int e[2050][2050];
int n,p,k;
bool vis[1050];
int dis[1050];
int tot;
bool Dijistra(int x){
    init(dis,INF);
    init(vis,0);
    dis[1]=0;
    rep(i,2,n)
    if(e[1][i]<INF)
        dis[i]=e[1][i]>=x?1:0;
    vis[1]=1;
    for(int i=1;i<n;i++){
        int minn=INF;
        int u;
        for(int j=1;j<=n;j++)
            if(!vis[j]&&minn>dis[j])
            minn=dis[u=j];
            if(u>n) return true;//这一句千万别忘了加上,否则会数组越界
        vis[u]=1;
        for(int j=1;j<=n;j++){
            if(e[u][j]<INF){
                int len=dis[u]+(e[u][j]>=x?1:0);
                if(dis[j]>len){
                    dis[j]=len;
                }
            }
        }
    }
    return dis[n]>k;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    scanf("%d%d%d",&n,&p,&k);
    rep(i,1,n)
    rep(j,1,n)
    if(i==j) e[i][j]=0;
    else e[i][j]=INF;
        int ub=1e6+2;int lb=0;
    rep(i,1,p) { int u,v,len;
        scanf("%d%d%d",&u,&v,&len);
        e[u][v]=e[v][u]=len;
        }
        int ans=0;
        while(ub>=lb){
            int mid=(lb+ub)>>1;
            if(Dijistra(mid)) ans=mid,lb=mid+1;
            else ub=mid-1;
        }
        if(ans>1e6) printf("-1\n");
        else printf("%d\n",ans);
    return 0;
}

但是上面的代码思路虽然是对的,最后显示Running Error,无语ing,找了好久才找的原因(—_—|||)

看了一些人的题解用链式前向星存储这一张图的

时间和空间复杂度明显降低了

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const int MOD=1e6;
const int INF= 0x3f3f3f3f;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define init(a,b) memset(a,b,sizeof a)
//int e[1050][1050];
int n,p,k;
bool vis[1050];
int dis[1050];
int head[1050];
struct node{
    int w,to,next;
}edge[20000];
int tot;
void add(int u,int v,int w)
{
    edge[tot].w=w;
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
bool Dijistra(int x){
    init(dis,INF);
    dis[1]=0;
    vis[1]=1;
    queue<int>q;
    q.push(1);
    while(!q.empty()){
            int p=q.front();q.pop();
    for(int i=head[p];i!=-1;i=edge[i].next){
        int tt=edge[i].to;
        int len=dis[p]+(edge[i].w>=x?1:0);
        if(dis[tt]>len){
            q.push(tt);
            dis[tt]=len;
            vis[tt]=1;

        }
    }
    }
    return dis[n]>k;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    scanf("%d%d%d",&n,&p,&k);
    init(head,-1);
        int ub=1e6+2;int lb=0;
    rep(i,1,p) { int u,v,len;
        scanf("%d%d%d",&u,&v,&len);
        add(u,v,len);
        add(v,u,len);
        }
        int ans=0;
        while(ub>=lb){
            int mid=(lb+ub)>>1;
            if(Dijistra(mid)) ans=mid,lb=mid+1;
            else ub=mid-1;
        }
        if(ans>1e6) printf("-1\n");
        else printf("%d\n",ans);
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值