CCF csp软件能力认证 第15次 第5题 管道清洁 java 100分

题目csp模拟考试系统201812-5的题目。

我的java张老师是负责csp的,于是java的一个作业就是做这一套题。用java写,行,c++转java而已,写算法题嘛,基本语法懂了就好了,就边学边做。结果第三题没有java的满分题解,或者是有但我没有找到。于是很苦逼,找到一个90分的,然后努力优化。因为我比较菜,日思夜想几天后优化完了。快乐地发了上一篇博客。

然后看到第五题,内心爆炸。觉得大概可以放弃了的时候,想到了自己在codeforces讨论群,里面都是大佬。于是抱着侥幸心理提了问,然后被蔡队回复了。

蔡队想了想撤回了后一条消息:“裸题”。

然后我就开始学。这里放一下我学的顺序,程序设计界有一个原则叫作“不要重复造轮子”,所以我也就不重复讲解算法了(其实就是懒)。

先学最大流,费用流,这两个我看了李煜东的书,然后出于好奇看了最小割,这是看大白书的(纯粹因为当时手边李煜东的那本不在),接下来就是上下界最小费用循环流,参考这个https://www.cnblogs.com/nietzsche-oier/p/8185805.html

就有思路了。建图和费用流用的李煜东的写法。

有问题存在,是我的加速读入里面,我用了先读String再取里面的元素作为char的方法,这样不知道会不会反而更慢,希望能学到加速char读入的方法,目前没找到。

所以这又是一个(或许是)全网首发csp第15次第5题满分题解!这回都不加java的头衔了,因为c++都没找到题解的说。作为蒟蒻真是容易满足啊,捡漏大佬懒得写的csp题解。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}

public class Main {
	static char nextChar() throws IOException{
    	String s = Reader.next();
    	return s.charAt(0);
    }
	
	public static int T = 0, S = 0, E = 0, mx = 4000, inf = 0x3f3f3f3f;
	public static int[] head = new int[mx];
	public static int[] Next = new int[mx];
	public static int[] cost = new int[mx];
	public static int[] ver = new int[mx];
	public static int[] edge = new int[mx];
	public static int tot, s, t, n, m, maxflow, ans;
	public static int[] d = new int[mx];
	public static int[] incf = new int[mx];
	public static int[] pre = new int[mx];
	public static int[] v = new int[mx];
	public static void init() {
		tot = 1; maxflow = 0; ans = 0;
		for(int i = 1; i <= n + 2; i++)
			head[i] = 0;
	}
	public static void add(int u, int v, int c, int val) {
		edge[++tot] = c; cost[tot] = val; ver[tot] = v;
		Next[tot] = head[u]; head[u] = tot;
	}
	public static void readin() throws IOException {
		n = Reader.nextInt(); m = Reader.nextInt();
		s = n + 1;
		t = n + 2;
		for(int i = 0; i < m; i++) {
			int u, v; char type;
			u = Reader.nextInt(); v = Reader.nextInt(); type = nextChar();
//			System.out.println(u + " " + v + " " + type + " ");
			switch(type) {
			case 'A':
				add(u, v, inf, E); add(v, u, 0, -E);
				add(u, t, 1, E); add(t, u, 0, -E);
				add(s, v, 1, 0); add(v, s, 0, 0);
				break;
			case 'B':
				add(u, t, 1, E); add(t, u, 0, -E);
				add(s, v, 1, 0); add(v, s, 0, 0);
				break;
			case 'C':
				add(u, v, inf, E); add(v, u, 0, -E);
				break;
			case 'D':
				add(u, v, 1, E); add(v, u, 0, -E);
				break;
			}
		}
	}
	
	static boolean spfa() {
		Queue<Integer> q = new LinkedList<Integer>();
		for(int i = 1; i <= n + 2; i++) {
			d[i] = inf;
			v[i] = 0;
		}
		q.add(s); d[s] = 0; v[s] = 1;
		incf[s] = 1 << 30;
		while(!q.isEmpty()) {
			int x = q.remove(); v[x] = 0;
			for(int i = head[x]; i != 0; i = Next[i]) {
				if(edge[i] == 0) continue;
				int y = ver[i];
				if(d[y] > d[x] + cost[i]) {
					d[y] = d[x] + cost[i];
					incf[y] = Math.min(incf[x], edge[i]);
					pre[y] = i;
					if(v[y] == 0) v[y] = 1;
					q.add(y);
				}
			}
		}
		if(d[t] == inf) return false;
		return true;
	}
	static void update() {
		int x = t;
		while(x != s) {
			int i = pre[x];
			edge[i] -= incf[t];
			edge[i ^ 1] += incf[t];
			x = ver[i ^ 1];
		}
		maxflow += incf[t];
//		System.out.println("d[t]" + d[t]);
		ans += d[t] * E;
	}
	static boolean leastfare() {
		while(spfa()) update();
		boolean can = true;
		for(int i = head[s]; i != 0; i = Next[i]) {
			if(edge[i] > 0) {
//				System.out.println("s ver:" + ver[i]);
				can = false;
				break;
			}
		}
		for(int i = head[t]; i != 0; i = Next[i]) {
			if(edge[i] <= 0) {
//				System.out.println("t ver:" + ver[i]);
				can = false;
				break;
			}
		}
		return can;
	}
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		T = Reader.nextInt(); S = Reader.nextInt(); E = Reader.nextInt();
		while(T-- > 0) {
			init();
			readin();
//			for(int i = head[s]; i != 0; i = Next[i]) {//检查建图
//				System.out.println("s to " + ver[i]);
//			}
			if(leastfare())
				System.out.println(ans);
			else System.out.println("-1");
		}
	}

}

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值