CCF csp software capabilities authentication sequence 5 15th title duct cleaning java 100 points

Topic title of csp mock exam system 201812-5.

My java Zhang is responsible for csp, then a job to do this java is a problem. Written in java, line, c ++ java turn only, write algorithms questions Well, to understand the basic syntax like, you learn by doing. The third problem is not the result of java out solution to a problem, or is there but I did not find. So bitter force, found a 90-point, then efforts to optimize. Because I am more vegetables, like night and day over a few days after the optimization. Happily made on a blog.

Then saw a fifth title, the heart explode. That probably gave up when the thought of his own in codeforces discussion groups, which are the big brother. So take chances put a question, then replied Cai team.

Cai thought the team withdrew after a message: "Naked problem."

Then I began to learn. Here let me learn about the order of the program design community there is a principle called "Do not repeat create the wheel," so I will not repeat explain the algorithm (in fact lazy).

First learn maximum flow, cost flow, both of which I read the book Li Yudong, and then out of curiosity looked minimum cut, which is to look at white book (purely because it was hand Li Yudong that this is not), the next step is upper and lower bounds minimum cost circulation flow, refer to this https://www.cnblogs.com/nietzsche-oier/p/8185805.html

There is a train of thought. Diagrams and flow with the construction cost of Li Yudong wording.

There are problems, I accelerated reading it, I used to read String then take the inside of the element as a method char, so do not know if it will but more slowly, hoping to learn ways to accelerate char read now did not find.

So this is a (perhaps) the whole network starting sequence csp 15 5 solution to a problem out of the question ! This time, do not add the title of java, c ++ because did not find the solution to a problem, he said. As konjac really easy to meet ah, too lazy to write Jianlou chiefs of csp explanations.

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

}

 

Guess you like

Origin blog.csdn.net/xuzonghao/article/details/89461145