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();
		    }
		}
	}

		

	
相关推荐
ん贤18 分钟前
贪心算法.
算法·贪心算法
cndes27 分钟前
大数据算法的思维
大数据·算法·支持向量机
睡不着还睡不醒2 小时前
【数据结构强化】应用题打卡
算法
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-05
人工智能·深度学习·神经网络·算法·机器学习·语言模型·自然语言处理
C++忠实粉丝3 小时前
前缀和(6)_和可被k整除的子数组_蓝桥杯
算法
木向3 小时前
leetcode42:接雨水
开发语言·c++·算法·leetcode
TU^3 小时前
C语言习题~day16
c语言·前端·算法
吃什么芹菜卷3 小时前
深度学习:词嵌入embedding和Word2Vec
人工智能·算法·机器学习
wclass-zhengge3 小时前
数据结构与算法篇(树 - 常见术语)
数据结构·算法
labuladuo5203 小时前
AtCoder Beginner Contest 372 F题(dp)
c++·算法·动态规划