Cattle-off practice match 43 c Tachibana Kanade Loves Review

Topic Link

Title Description

Li Hua playing is just starting to learn a new OI adorable.
Recently, powerful Qingyu elected the topic of human IODS 9102. As we all know, IODS is an extremely malignant tumor of the game. In order to achieve good results in this competition, Li Hua playing decided to learn every possible knowledge to the test.
In Qingyu's blog, Li Hua playing that game players in total will visit n knowledge points. Earlier, Li Hua playing has learned to rely on self-study, where k knowledge points. Next, Li Hua playing to learn other points of knowledge, learning each a single point of knowledge, time consuming for Ti days. At the same time, there is a link between certain knowledge, you can speed up the learning process. After calculation, Li Hua played a total of found where m kinds of contact, the i-th link can be expressed as (Xi, Yi, Hi), which means "any mastered the first Xi knowledge points and Yi knowledge points in a after learning to master another Hi-day knowledge. "
Left to Li Hua playing time is running out, only t days, so she wanted to know he could not learn in this t days to complete all the knowledge points.

Enter a description:

This large amount of input question, note that the use of higher efficiency mode reads
the first line of input contains four integers n, m, k, t, the meaning supra.
The next line contains n integers, respectively for T1, T2, ⋯, Tn
next line contains k integers represent knowledge Lihua played've learned about already. If k = 0, here it is a blank line.
Next m lines of three integers Xi, Yi, Hi, describes a contact.

Output Description:

If Li Hua finished playing can learn all the knowledge points, one line of output Yes. Otherwise, output No

Example 1

Entry

4 3 2 5
4 5 6 7
2 3
1 2 3
1 3 2
3 4 2

Export

Yes

Explanation

Li Hua playing already learned the first two, three knowledge by the first two relations, Li Hua playing can take two days to learn the knowledge point 1, by the relation 3, Li Hua playing can two days to learn the knowledge point 4, so a total it takes four days to complete the task.

Example 2

Entry

5 4 0 12
4 5 6 7 1
 
1 2 3
1 3 2
3 4 2
1 5 233

Export

Yes

Explanation

Li Hua playing more vegetables, so nothing learned. She can choose to spend the first four days of time to learn the knowledge point 1. Then the relationship 1, 2, 3, respectively, to spend two days time to learn knowledge points 2, 3, 4 and then learn knowledge based on the relationship between 3 and spend two days time. Then she re-learning knowledge alone 5, spend one day, spent a total of 12 days to complete the task.
Note that although the relationship between 4 Yun Xu Lihua played learn the basics of point 5 point 1 on, but the time required even more than learning alone, so Li Hua playing not learn the basics of the knowledge point 1 point 5 .

Remarks:

0⩽k⩽n⩽1e6, m⩽5e6, t⩽1e18, Ti, Hi⩽1e3

It can be converted to minimum spanning tree problem.

java had failed due to timeout. . . It may be a bit slow to read it. . .
This is what I currently can minimize the constant. . .

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	static StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in));

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = nextNum();
		int m = nextNum();
		int k = nextNum();
		long t = nextNum();
		int T[] = new int[n + 1];
		PriorityQueue<Node> pq = new PriorityQueue<>();
		for (int i = 1; i <= n; i++) {
			T[i] = nextNum();
			pq.add(new Node(0, i, T[i], 0));
		}
		boolean has[] = new boolean[n + 1];
		boolean use[] = new boolean[m + 1];
		has[0] = true;
		int K[] = new int[k];
		for (int i = 0; i < k; i++) {
			K[i] = nextNum();
			has[K[i]] = true;
		}

		ArrayList<ArrayList<Node>> al = new ArrayList<>();
		for (int i = 0; i <= n; i++) {
			al.add(new ArrayList<>());
		}
		int flag = k;
		for (int i = 1; i <= m; i++) {
			Node node = new Node(nextNum(), nextNum(), nextNum(), i);
			al.get(node.x).add(node);
			al.get(node.y).add(node);
		}

		for (int i = 0; i < k; i++) {
			for (Node node : al.get(K[i])) {
				if (!use[node.id]) {
					pq.add(node);
					use[node.id] = true;
				}
			}
		}

		long ans = 0;
		while (!pq.isEmpty()) {
			Node te = pq.poll();
			if (has[te.x] && has[te.y]) {
				continue;
			} else if (has[te.x]) {
				flag++;
				ans += te.h;
				has[te.y] = true;
				for (Node node : al.get(te.y)) {
					if ((has[node.x] && has[node.y]) || use[node.id]) {
						continue;
					} else {
						pq.add(node);
						if(node.id!=0) {
							use[node.id] = true;
						}
					}
				}
			} else {
				flag++;
				ans += te.h;
				has[te.x] = true;
				for (Node node : al.get(te.x)) {
					if ((has[node.x] && has[node.y]) || use[node.id]) {
						continue;
					} else {
						pq.add(node);
						if(node.id!=0) {
							use[node.id] = true;
						}
					}
				}
			}
			if (flag >= n) {
				break;
			}
			if (ans > t) {
				System.out.println("No");
				return;
			}
		}
		System.out.println("Yes");
		return;
	}
	private static int nextNum() {
		try {
			st.nextToken();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (int) st.nval;

	}
}

class Node implements Comparable<Node> {
	int x;
	int y;
	int h;
	int id;

	public Node(int x, int y, int h, int id) {
		super();
		this.x = x;
		this.y = y;
		this.h = h;
		this.id = id;
	}

	@Override
	public String toString() {
		return "Node [x=" + x + ", y=" + y + ", h=" + h + "]";
	}

	@Override
	public int compareTo(Node o) {

		return h - o.h;
	}

}
Published 50 original articles · won praise 32 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_40827780/article/details/89075850