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;
    }
    
    
  • 相关阅读:
    SharePoint 疑难杂症之加载控件TaxonomyPicker.ascx失败及其解决方法
    SQL Server、Oracle、db2所提供的简装版(Express)比较
    试水Windows 8 Metro application(xaml)及我的一些理解
    基于OAuth实现的Windows Live Writer新浪微博插件
    fmplan主页功能设计第一阶段成果
    MVC3课程中的几个问题整理
    【十五分钟Talkshow】为什么新浪微博的输入文本框具有记忆功能
    参照WPF实现Silverlight中的多值绑定特性
    MVVM、MVVMLight、MVVMLight Toolkit之我见
    面向服务的RIA应用系统开发中的异常处理
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7498579.html
Copyright © 2011-2022 走看看