P1948 [USACO08JAN] Telephone Lines S

Here

典中之典!!

解题思路

  • 可选k条边代价为0
  • 如何决策?
  • 将到当前位置选择了几条代价为0的边放入状态,即
  • 若当前状态选的边数小于,则可以进行决策,是否选择当前边,
  • 若选,则,否则
  • 若当前状态选的边数等于,则只能
  • 考虑分层图,建k层
  • (实际上就是将上一种方法中多一维记录来区别状态),改为用点的标号区分)
  • 对于每条边,层连一条相同但代价为0的边
  • 同层连代价为的边
  • 最终输出第k层的
java 复制代码
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);
		}
	}
  • 由于代价具有单调性(代价低的,花费更高代价一定也行)
  • 二分答案
  • 对于每条边的边权有小到大排序去重 ,将边权视为答案进行
  • 时,对大于当前所选答案的边,视作代价为1的边,小于的视作代价为0
  • 判断最终,即为可能解,接着往小进行尝试
  • 否则,往大进行尝试
java 复制代码
//二分答案
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();
		    }
		}
	}

		

	
相关推荐
学不动CV了4 小时前
C语言32个关键字
c语言·开发语言·arm开发·单片机·算法
小屁孩大帅-杨一凡5 小时前
如何解决ThreadLocal内存泄漏问题?
java·开发语言·jvm·算法
Y1nhl6 小时前
力扣_二叉树的BFS_python版本
python·算法·leetcode·职场和发展·宽度优先
向阳逐梦8 小时前
PID控制算法理论学习基础——单级PID控制
人工智能·算法
2zcode8 小时前
基于Matlab多特征融合的可视化指纹识别系统
人工智能·算法·matlab
Owen_Q8 小时前
Leetcode百题斩-二分搜索
算法·leetcode·职场和发展
矢志航天的阿洪8 小时前
蒙特卡洛树搜索方法实践
算法
UnderTheTime9 小时前
2025 XYD Summer Camp 7.10 筛法
算法
zstar-_9 小时前
Claude code在Windows上的配置流程
笔记·算法·leetcode
圆头猫爹9 小时前
第34次CCF-CSP认证第4题,货物调度
c++·算法·动态规划