P1948 [USACO08JAN] Telephone Lines S

文章介绍了在给定图中,如何通过构建分层图并利用Dijkstra算法,结合边权的单调性,采用二分查找策略来决策如何选择k条代价为0的边的问题解法。
摘要由CSDN通过智能技术生成

Here

典中之典!!

解题思路

  • 可选k条边代价为0
  • 如何决策?

 1.

  • 将到当前位置选择了几条代价为0的边放入状态,即dis[u][cnt],Node(u,cnt,dis)
  • 若当前状态选的边数小于k,则可以进行决策,是否选择当前边,
  • 若选,则cnt+1,dis+0,否则cnt,Math.max(dis,w_{u->v})
  • 若当前状态选的边数等于k,则只能cnt,Math.max(dis,w_{u->v})

2.

  • 考虑分层图,建k层
  • (实际上就是将上一种方法中多一维记录cnt来区别状态),改为用点的标号区分)
  • 对于每条边,ii+1层连一条相同但代价为0的边
  • 同层连代价为w的边
  • 最终输出第k层的dis[n]
for(int i=1;i<=m;++i){
		int x=read(),y=read(),w=read();
		for(int j=0;j<=k;++j){
			Lian(x+j*n,y+j*n,w);
			Lian(y+j*n,x+j*n,w);
			if(j==k)continue;
			Lian(x+j*n,y+(j+1)*n,0);
			Lian(y+j*n,x+(j+1)*n,0);
		}
	}

3. 

  • 由于代价具有单调性(代价低的,花费更高代价一定也行)
  • 二分答案
  • 对于每条边的边权有小到大排序去重 ,将边权视为答案进行check
  • check时,对大于当前所选答案的边,视作代价为1的边,小于的视作代价为0
  • 判断最终dis[n]\leq k,即为可能解,接着往小进行尝试
  • 否则,往大进行尝试
//二分答案
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;








//implements Runnable
public class Main{
	static long md=(long)20020219;
	static long Linf=Long.MAX_VALUE/2;
	static int inf=Integer.MAX_VALUE/2;
	static int M=10000;
	static int N=1000;
	static int n=0;
	static int m=0;
	static int k=0;
	static 
	class Edge{
		int fr,to,val,nxt;
		public Edge(int u,int v,int w) {
			fr=u;
			to=v;
			val=w;
		}
	}
	static Edge[] e=new Edge[2*M+1];
	static int[] head=new int[N+1];
	static int cnt=0;
	static void addEdge(int fr,int to,int val) {
		cnt++;
		e[cnt]=new Edge(fr, to, val);
		e[cnt].nxt=head[fr];
		head[fr]=cnt;
	}
	
	static
	class Node{
		int x;
		int y;
		public Node(int u,int v) {
			x=u;
			y=v;
		}
	}
	static boolean Dij(int s,int lim) {
		int[] dis=new int[N+1];
		Arrays.fill(dis, inf);
		boolean[] vis=new boolean[N+1];
		PriorityQueue<Node> q=new PriorityQueue<Node>((o1,o2)->{
			return o1.y-o2.y;
			
		});
		q.add(new Node(s, 0));
		dis[s]=0;
		while(!q.isEmpty()) {
			Node now=q.peek();
			q.poll();
			int x=now.x;
			if(vis[x])continue;
			vis[x]=true;
			for(int i=head[x];i>0;i=e[i].nxt) {
				int v=e[i].to;
				if(vis[v])continue;
				int w=e[i].val;
				if(w>lim) {
					if(dis[x]+1<dis[v]) {
						dis[v]=dis[x]+1;
						q.add(new Node(v, dis[v]));
					}
				}else {
					if(dis[x]<dis[v]) {
						dis[v]=dis[x];
						q.add(new Node(v, dis[v]));
					}
				}
			}
		}
		if(dis[n]<=k)return true;
		else return false;
	}
	
	public static void main(String[] args) throws Exception{
		AReader input=new AReader();
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));	
			
		n=input.nextInt();
		m=input.nextInt();
		k=input.nextInt();
		int[] a=new int[m+1];
		int aa=0;
		int[] b=new int[m+1];
		int bb=0;
		for(int i=1;i<=m;++i) {
			int u=input.nextInt();
			int v=input.nextInt();
			int w=input.nextInt();
			addEdge(u, v, w);
			addEdge(v, u, w);
			aa++;
			a[aa]=w;
		}
		Arrays.sort(a,1,aa+1);
		for(int i=1;i<=aa;++i) {
			if(a[i]!=a[i-1]) {
				bb++;
				b[bb]=a[i];
			}
		}
		int l=1,r=bb;
		int ans=-1;
		while(l<=r) {
			int mid=(l+r)>>1;
			if(Dij(1, b[mid])) {
				ans=b[mid];
				r=mid-1;
			}else l=mid+1;
		}
		out.print(ans);
		out.flush();
	    out.close();
	}
//	public static final void main(String[] args) throws Exception {
//		  new Thread(null, new Main(), "线程名字", 1 << 27).start();
//	}
//		@Override
//		public void run() {
//			try {
//				//原本main函数的内容
//				solve();
//
//			} catch (Exception e) {
//			}
//		}
//	static 
//	class Node{
//		int x;
//		int y;
//		public Node(int u,int v) {
//			x=u;
//			y=v;
//		}
//		@Override
//	    public boolean equals(Object o) {
//	        if (this == o) return true;
//	        if (o == null || getClass() != o.getClass()) return false;
//	        Node may = (Node) o;
//	        return x == may.x && y==may.y;
//	    }
//
//	    @Override
//	    public int hashCode() {
//	        return Objects.hash(x, y);
//	    }
//	}
		static
		class AReader{ 
		    BufferedReader bf;
		    StringTokenizer st;
		    BufferedWriter bw;

		    public AReader(){
		        bf=new BufferedReader(new InputStreamReader(System.in));
		        st=new StringTokenizer("");
		        bw=new BufferedWriter(new OutputStreamWriter(System.out));
		    }
		    public String nextLine() throws IOException{
		        return bf.readLine();
		    }
		    public String next() throws IOException{
		        while(!st.hasMoreTokens()){
		            st=new StringTokenizer(bf.readLine());
		        }
		        return st.nextToken();
		    }
		    public char nextChar() throws IOException{
		        //确定下一个token只有一个字符的时候再用
		        return next().charAt(0);
		    }
		    public int nextInt() throws IOException{
		        return Integer.parseInt(next());
		    }
		    public long nextLong() throws IOException{
		        return Long.parseLong(next());
		    }
		    public double nextDouble() throws IOException{
		        return Double.parseDouble(next());
		    }
		    public float nextFloat() throws IOException{
		        return Float.parseFloat(next());
		    }
		    public byte nextByte() throws IOException{
		        return Byte.parseByte(next());
		    }
		    public short nextShort() throws IOException{
		        return Short.parseShort(next());
		    }
		    public BigInteger nextBigInteger() throws IOException{
		        return new BigInteger(next());
		    }
		    public void println() throws IOException {
		        bw.newLine();
		    }
		    public void println(int[] arr) throws IOException{
		        for (int value : arr) {
		            bw.write(value + " ");
		        }
		        println();
		    }
		    public void println(int l, int r, int[] arr) throws IOException{
		        for (int i = l; i <= r; i ++) {
		            bw.write(arr[i] + " ");
		        }
		        println();
		    }
		    public void println(int a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(int a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(String a) throws IOException{
		        bw.write(a);
		        bw.newLine();
		    }
		    public void print(String a) throws IOException{
		        bw.write(a);
		    }
		    public void println(long a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(long a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(double a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(double a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void print(char a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(char a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		}
	}

		

	

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值