P10166 [DTCPC 2024] 环

news2024/9/24 1:27:20

 解题思路

  • 满足p_1,p_2\cdots p_k的序列即为环
  • 若图上有环,则代价为0
  • 若无环,则在图上已有的边在添加一条回边可形成环
  • 对所有的点由小到大排序
  • 由于环的大小可以为2
  • ans初值设为(a_1+a_2)*2,先不管最小点之间是否有边,反正不会更劣
  • 通过拓扑排序找环,在找环的过程中,看能否添一条边,代价更小
  • 若拓扑不能访问到所有的点则有环
  • 添边,则按照拓扑顺序,维护每个点能被访问的点中,值最小的与其连边形成环,判断代价

 


import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;







public class Main{
	static long md=(long)998244353;
	static long Linf=Long.MAX_VALUE/2;
	static int inf=Integer.MAX_VALUE/2;
	
	static
	class Edge{
		int fr,to,nxt;
		public Edge(int u,int v) {
			fr=u;
			to=v;
		}
	}
	static Edge[] e;
	static int[] head;
	static int cnt=0;
	static void addEdge(int fr,int to) {
		cnt++;
		e[cnt]=new Edge(fr, to);
		e[cnt].nxt=head[fr];
		head[fr]=cnt;
	}
	static 
	class Node{
		int x;
		long y;
		public Node(int X,long Y) {
			x=X;
			y=Y;
		}
	}
	
	
	public static void main(String[] args) throws IOException{
		AReader input=new AReader();
	    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
	    int n=input.nextInt();
	    int m=input.nextInt();
	    e=new Edge[m+1];
	    head=new int[n+1];
	    cnt=0;
	    long[] a=new long[n+1];
	    long[] b=new long[n+1];
	    for(int i=1;i<=n;++i) {
	    	long y=input.nextLong();
	    	a[i]=y;
	    	b[i]=y;
	    }
	    Arrays.sort(a,1,n+1);
	    //拓扑排序
	    int[] in=new int[n+1];
	    for(int i=1;i<=m;++i) {
	    	int u=input.nextInt();
	    	int v=input.nextInt();
	    	addEdge(u, v);
	    	in[v]++;
	    }
	    Queue<Integer> q=new LinkedList<Integer>();
	    int tot=0;
	    long[] c=new long[n+1];
	    Arrays.fill(c, Linf);
	    for(int i=1;i<=n;++i) {
	    	if(in[i]==0) {
	    		q.add(i);
	    		
	    	}
	    }
	    
	    while(!q.isEmpty()) {
	    	int x=q.peek();
	    	q.poll();
	    	tot++;
	    	for(int i=head[x];i>0;i=e[i].nxt) {
	    		int v=e[i].to;
	    		c[v]=Math.min(Math.min(c[x], b[x]), c[v]);
                //维护每个点能被访问点中最小的值、
                //c[x]不包含自己,在更新其连边时要加上

	    		in[v]--;
	    		if(in[v]==0) {
	    			
	    			q.add(v);
	    		}
	    	}
	    }
	    
	    if(tot<n) {//判环
	    	out.println(0);
	    }else {//无环
	    	long ans=(a[1]+a[2])*2;
	    	for(int i=1;i<=n;++i) {
	    		long res=b[i]+c[i];
	    		ans=Math.min(ans, res);
	    		
	    	}
	    	out.print(ans);
	    }
	    
 	    out.flush();
	    out.close();
	}
	//System.out.println();
	//out.println();
	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();
	    }
	}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1483706.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Vue3+vite打包后页面空白问题

vite.config.js vite.config.js 增加 base: ./ import { fileURLToPath, URL } from node:url import { defineConfig } from vite import vue from vitejs/plugin-vue// https://vitejs.dev/config/ export default defineConfig({base: ./,resolve: {alias: {: fileURLToPath…

Mysql学习之各种锁

锁 事务的隔离性由锁来实现 MySQL并发事务访问相同记录 并发事务访问相同记录的情况大致可以分为3种&#xff1a; 读-读的情况 读-读情况&#xff0c;即并发事务相继读取相同的记录。读取操作本身不会对记录由有任何的影响&#xff0c;并不会引起什么问题&#xff0c;所以允许…

git代码上库流程(一篇就够了)

文章目录 一、前言二、创建新分支三、修改代码四、合并分支 一、前言 多人协同开发一个项目&#xff0c;为了方便管理代码&#xff0c;每个人代码提交不冲突。git代码仓库管理是不可或缺的。对于新手来说&#xff0c;既不懂git原理又不懂底层逻辑&#xff0c;还经常错误提交代码…

10s用递归实现反转链表(JAVA)

目录 1. 原理 1.1 问题 1.2 解决 1.3 终点 2.代码操作及讲解 2.1 演示代码 2.2 讲解 1. 原理 反转 A->B->C 1.1 问题 当反转A->B的链接时&#xff0c;当B改成指向A&#xff08;B->A&#xff09;, 就找不到C了&#xff1b; 1.2 解决 所以需要一个变量临时…

uniapp_小程序选项卡(直接下载安装即可)

选项卡 - DCloud 插件市场 也可加一行变成三栏

Java集合相关面试题(2024大厂高频面试题系列)

1、说一说Java提供的常见集合&#xff1f;&#xff08;画一下集合结构图&#xff09; 在java中提供了量大类的集合框架&#xff0c;主要分为两类&#xff1a; 第一个是Collection 属于单列集合&#xff0c;第二个是Map 属于双列集合 在Collection中有两个子接口List和Set。…

Kubernetes/k8s的核心概念

一、什么是 Kubernetes Kubernetes&#xff0c;从官方网站上可以看到&#xff0c;它是一个工业级的容器编排平台。Kubernetes 这个单词是希腊语&#xff0c;它的中文翻译是“舵手”或者“飞行员”。在一些常见的资料中也会看到“ks”这个词&#xff0c;也就是“k8s”&#xff…

Page Object模式:为什么它是Web自动化测试的必备工具

为 UI 页面写测试用例时&#xff08;比如 web 页面&#xff0c;移动端页面&#xff09;&#xff0c;测试用例会存在大量元素和操作细节。当 UI 变化时&#xff0c;测试用例也要跟着变化&#xff0c; PageObject 很好的解决了这个问题。 使用 UI 自动化测试工具时&#xff08;包…

电子电气架构——汽车以太网诊断路由汇总

电子电气架构——汽车以太网诊断路由汇总 我是穿拖鞋的汉子,魔都中坚持长期主义的工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 人们会在生活中不断攻击你。他们的主要武器是向你灌输对自己的怀疑:你的价值、你的能力、你的潜力。他们往往会将…

lottie加载带图片的json 预览

背景 产品看到一款app的动效很不错&#xff0c;让我去模仿实现。 第一步 获取apk中的静态资源 拿到这个app的apk后&#xff0c;直接使用压缩工具解压&#xff0c; assets文件夹就是静态资源的目录 静态资源里面有lottie 那么大部分的动效应该都是lottie实现的 网上找了很多…

蓝桥杯Java B组历年真题(2013年-2021年)

一、2013年真题 1、世纪末的星期 使用日期类判断就行&#xff0c;这里使用LocalDate&#xff0c;也可以使用Calendar类 答案 2099 使用LocalDate import java.time.LocalDate; import java.time.format.DateTimeFormatter; // 1:无需package // 2: 类名必须Main, 不可修改p…

配置MySQL与登录模块

使用技术 MySQL&#xff0c;Mybatis-plus&#xff0c;spring-security&#xff0c;jwt验证&#xff0c;vue 1. 配置Mysql 1.1 下载 MySQL :: Download MySQL Installer 1.2 安装 其他页面全选默认即可 1.3 配置环境变量 将C:\Program Files\MySQL\MySQL Server 8.0\bin…

Flink状态存储-StateBackend

文章目录 前言一、MemoryStateBackend二、FSStateBackend三、RocksDBStateBackend四、StateBackend配置方式五、状态持久化六、状态重分布OperatorState 重分布KeyedState 重分布 七、状态过期 前言 Flink是一个流处理框架&#xff0c;它需要对数据流进行状态管理以支持复杂的…

猫头虎分享已解决Bug || AttributeError: ‘str‘ object has no attribute ‘decode‘

博主猫头虎的技术世界 &#x1f31f; 欢迎来到猫头虎的博客 — 探索技术的无限可能&#xff01; 专栏链接&#xff1a; &#x1f517; 精选专栏&#xff1a; 《面试题大全》 — 面试准备的宝典&#xff01;《IDEA开发秘籍》 — 提升你的IDEA技能&#xff01;《100天精通鸿蒙》 …

pycharm专业版本的安装

一 、到官网下载对应的pycharm安装包 也可以把安装软件&#xff08;用物理机下载到共享文件夹&#xff09; 然后进入Ubuntu系统把下载大的安装包剪贴到目标路径 1 在ubuntu中创建一个用来存放pycharm安装包的文件夹 rootzmq-virtual-machine:/home/zmq/Desktop# mkdir pycha…

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的教室人员检测与计数(Python+PySide6界面+训练代码)

摘要&#xff1a;开发教室人员检测与计数系统对于优化教学资源和提升教学效率具有重要意义。本篇博客详细介绍了如何利用深度学习构建此系统&#xff0c;并提供了完整的实现代码。该系统基于强大的YOLOv8算法&#xff0c;并对比了YOLOv7、YOLOv6、YOLOv5的性能&#xff0c;展示…

初识Maven

介绍&#xff1a; web后端开发技术ApacheMaven是一个项目管理和构建工具&#xff0c;它基于项目对象模型&#xff08;POM&#xff09;的概念&#xff0c;通过一小段描述信息来管理项目的构建。安装&#xff1a;http://maven.apache.org/ Apache软件基金会&#xff0c;成立于19…

Leetcode42. 接雨水 -hot100

题目&#xff1a; 代码(首刷自解 2024年3月2日 有3个案例超时&#xff09;&#xff1a; 不算完全做出来&#xff0c;看了答案了&#xff0c;等以后二刷吧 class Solution { public:int helper(const vector<int>& height,const int high) {for(int i 0; i < hei…

对单元测试的思考(稳定性建设)

单测是很常见的技术的名词&#xff0c;但背后的逻辑和原理你是否清楚&#xff0c;让我们一起review一下。 1. 单测是什么&#xff1f;&#x1f914; 单测是单元测试,主要是测试一个最小逻辑块。比如一个函数、一个react、vue 组件。 2.为什么要写单测&#xff1f;&#x1f9…

扩展学习|大数据分析的现状和分类

文献来源&#xff1a;[1] Mohamed A , Najafabadi M K , Wah Y B ,et al.The state of the art and taxonomy of big data analytics: view from new big data framework[J].Artificial Intelligence Review: An International Science and Engineering Journal, 2020(2):53. 下…