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;
    }
    
    
  • 相关阅读:
    2019-2020-1 20175320 《信息安全系统设计基础》第五周学习总结
    2019-2020-1 20175320 《信息安全系统设计基础》第四周学习总结
    2019-2020-1 20175320 《信息安全系统设计基础》第三周学习总结
    2018-2019-2 20175320实验五《网络编程与安全》实验报告
    20175320个人项目-趣味自然数
    MySort
    2018-2019-2 20175320实验四《Android程序设计》实验报告
    20175320 2018-2019-2 《Java程序设计》第11周学习总结
    20175320 2018-2019-2 《Java程序设计》第10周学习总结
    2018-2019-2 20175320实验三《敏捷开发与XP实践》实验报告
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7498579.html
Copyright © 2011-2022 走看看