zoukankan      html  css  js  c++  java
  • POJ 3662 Telephone Lines

    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.

    题目大意:N的点M条边的图,求1到n路径中最长边的最小值,并且可以选择K条边权值变为0

    解题报告:显然是需要二分答案的,那么建图之后我们就开始跑spfa,spfa的状态定义为到i点最少需要几条边免费,如果跑出来的值>K 那么就不合法,参考模型:通往奥格瑞玛的路

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    const int N=1005,M=10005,inf=1000000;
    int n,m,K,head[N],num=0,nxt[M<<1],to[M<<1],dis[M<<1];
    void link(int x,int y,int z){
    	nxt[++num]=head[x];to[num]=y;dis[num]=z;head[x]=num;
    }
    int f[N],q[N*10],mod=N*10;bool vis[N];
    bool check(int mid){
    	int t=0,sum=1,x,u,v;
    	for(int i=0;i<=n;i++)f[i]=inf,vis[i]=false;
    	f[1]=0;vis[1]=true;q[1]=1;
    	while(t!=sum){
    		t++;if(t==mod)t=0;x=q[t];
    		for(int i=head[x];i;i=nxt[i]){
    			u=to[i];
    			v=dis[i]<=mid?0:1;
    			if(f[x]+v<f[u]){
    				f[u]=f[x]+v;
    				if(!vis[u]){
    					vis[u]=true;
    					sum++;if(sum==mod)sum=0;q[sum]=u;
    				}
    			}
    		}
    		vis[x]=false;
    	}
    	return f[n]<=K;
    }
    void work()
    {
    	int x,y,z;
    	scanf("%d%d%d",&n,&m,&K);
    	for(int i=1;i<=m;i++){
    		scanf("%d%d%d",&x,&y,&z);
    		link(x,y,z);link(y,x,z);
    	}
    	int l=0,r=inf,mid,ans=-1;
    	while(l<=r){
    		mid=(l+r)>>1;
    		if(check(mid))ans=mid,r=mid-1;
    		else l=mid+1;
    	}
    	printf("%d
    ",ans);
    }
    
    int main()
    {
    	work();
    	return 0;
    }
    
    
  • 相关阅读:
    大数据之MapReduce工作机制
    swoft +nginx 快速获取客户端的真实的IP
    JWT 介绍 详解
    swoft 切面编程 (AOP编程思想)
    CSP 编程模型
    Ubuntu 16.04 Mysql 重置密码
    ubuntu 提示:rm: cannot remove 'you-get/tmp': Directory not empty
    Ubuntu 系统升级到php7.2/7.3 (平滑升级)-朝花夕拾
    git clone 非标准的ssh端口(非22)端口
    Ubuntu 升级npm 以及安装cross-env 过程中遇到的问题
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7498579.html
Copyright © 2011-2022 走看看