UVa452 - Project Scheduling(AOE问题)

 Project Scheduling 

A project management technique called Pert involves breaking a largeproject into a number of tasks, estimating the time required to perform eachtask, and determining which tasks can not be started until others have beencompleted. The project is then summarized in chart form. For example, thechart (which corresponds to the sample input below)

indicates that tasks A, B, C, D, E and F each take 5, 3, 2, 2, 4, and 2 days respectively, that task E cannot complete until C and D are both completed, but that D can be performed in parallel with B and C.

Write a program that accepts a Pert chart and computes the amountof time required to complete a project.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Input will be from 1 to 27 lines, each corresponding to a different task. Each line will contain:

  1. A single upper case letter serving as the name of a task.
  2. An integer indicating the number of days required to complete that task.
  3. 0-26 additional uppercase letters, each indicating another task that must complete before this one can begin.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

The output is a single integer indicating the amount of time that will pass before all tasks can complete.

Sample Input

2

A 5
B 3 A
D 2 A
C 2 B
F 2 CE
E 4 DC

A 5
B 3 A
D 2 A
C 2 B
F 2 CE
E 4 DC

Sample Output

16

16
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.Arrays;

public class Main 
{
	public static final boolean DEBUG = false;
	public static final int N = 26;
	public BufferedReader cin;
	public PrintWriter cout;
	public HashMap<Character, Integer> chMap = new HashMap<Character, Integer>();
	public int[][] g = new int[N][N];
	public int[] t = new int[N];
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next()
	{ 
		try {
			return cin.readLine();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void input() 
	{
		String s;
		StringTokenizer tokenizer;
		
		for (int i = 0; i < N; i++) Arrays.fill(g[i], 0);
		Arrays.fill(t, 0);
		
		chMap.clear();
		while (true) {
			s = next();
			if (s == null) break;
			else if (s.compareTo("") == 0) break;
			else {
				tokenizer = new StringTokenizer(s);
				int cnt = 0;
				int u = 0, v = 0;
				
				while (tokenizer.hasMoreTokens()) {
					String tmp = tokenizer.nextToken();
					char ch = tmp.charAt(0);
					if (Character.isLetter(ch)) {
						if (cnt == 0) {
							if (chMap.containsKey(ch)) {
								u = chMap.get(ch);
							} else {
								u = chMap.size();
								chMap.put(ch, u);
							}
						} else if (cnt == 2) {
							for (int i = 0, len = tmp.length(); i < len; i++) {
								ch = tmp.charAt(i);
								if (chMap.containsKey(ch)) {
									v = chMap.get(ch);
								} else {
									v = chMap.size();
									chMap.put(ch, v);
								}
								g[v][u] = 1;
							}
							
						}
					} else {
						t[u] = Integer.parseInt(tmp);
					}
					
					cnt++;
				}
			}
		}
		
	}

	
	
	public void solve(int cas) 
	{
		int size = chMap.size();
		int[] inDegree = new int[size];
		
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if (g[i][j] > 0) inDegree[j]++;
			}
		}
		
		int[] topoSeq = new int[size];
		for (int i = 0; i < size; i++) {
			int j = 0;
			while (j < size && inDegree[j] != 0) j++;
			if (j >= size) break;
			topoSeq[i] = j;
			inDegree[j] = 0xff;
			for (int k = 0; k < size; k++) {
				if (g[j][k] > 0) inDegree[k]--;
			}
		}
		
		
		int[] b = new int[size], p = new int[size];
		for (int i = 0; i < size; i++) {
			b[topoSeq[i]] = t[topoSeq[i]];
			p[topoSeq[i]] = -1;
		}
		
		
		for (int i = 1; i < size; i++) {
			for (int j = 0; j < i; j++) {
				if (g[topoSeq[j]][topoSeq[i]] > 0) {
					if (b[topoSeq[j]] + t[topoSeq[i]] > b[topoSeq[i]]) {
						b[topoSeq[i]] = b[topoSeq[j]] + t[topoSeq[i]];
						p[topoSeq[i]] = topoSeq[j];
					}
				}
			}
		}
		
		
		
		int ans = Integer.MIN_VALUE;
		for (int i = 0; i < size; i++) {
			ans = Math.max(ans, b[i]);
		}
		cout.println(ans);
		
		if (cas != 0) cout.println();
		cout.flush();
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		int t = Integer.parseInt(solver.next());
		solver.next();
		while (t-- > 0) {
			solver.input();
			solver.solve(t);
		}
	}
}



### Spring Boot Starter Scheduling 使用与配置 Spring Boot 提供了一个强大的工具来简化定时任务的开发,即 `spring-boot-starter` 中内置的支持调度功能。以下是关于如何启用和配置调度功能的相关说明。 #### 启用调度支持 为了在应用程序中使用调度功能,需要确保项目依赖项中包含了 `spring-boot-starter` 的核心库。默认情况下,Spring Boot 已经提供了对调度的支持,只需引入以下 Maven 或 Gradle 依赖即可: 对于 Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> ``` 对于 Gradle: ```gradle implementation 'org.springframework.boot:spring-boot-starter' ``` #### 配置调度器 要在 Spring Boot 应用程序中启用调度功能,需在主类上添加 `@EnableScheduling` 注解。此注解会激活 Spring 的计划任务机制[^1]。 示例代码如下: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 创建定时任务 创建一个服务类并定义定时执行的方法。可以使用 `@Scheduled` 注解指定任务的触发条件。常见的触发方式包括固定延迟 (`fixedDelay`) 和 Cron 表达式 (`cron`)。 示例代码展示了一种基于固定时间间隔的任务调度: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("当前时间为:" + System.currentTimeMillis()); } @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次 public void fixedCronJob() { System.out.println("每分钟打印一次日志"); } } ``` 上述代码中的第一个方法每隔 5 秒钟运行一次,而第二个方法则按照设定的 Cron 时间表达式定期运行[^2]。 #### 自定义线程池 如果希望自定义用于调度任务的线程池,可以通过实现 `ThreadPoolTaskExecutor` 来完成这一目标。例如,可以在配置类中定义一个 Bean 并设置其属性以满足特定需求。 示例代码如下: ```java import org.springframework.context.annotation.Bean; import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration public class TaskConfig { @Bean(name = {"applicationTaskExecutor", "taskExecutor"}) public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(10); executor.setQueueCapacity(25); executor.initialize(); return executor; } } ``` 以上代码片段展示了如何通过注入的方式定制化线程池大小和其他参数[^3]。 --- #### 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值