列表

详情


NC24390. [USACO 2013 Feb S]Milk Scheduling

描述

Farmer John's N cows (1 <= N <= 10,000) are conveniently numbered 1..N. Each cow i takes T(i) units of time to milk. Unfortunately, some cows must be milked before others, owing to the layout of FJ's barn. If cow A must be milked before cow B, then FJ needs to completely finish milking A before he can start milking B. 
In order to milk his cows as quickly as possible, FJ has hired a large number of farmhands to help with the task -- enough to milk any number of cows at the same time. However, even though cows can be milked at the same time, there is a limit to how quickly the entire process can proceed due to the constraints requiring certain cows to be milked before others. Please help FJ compute the minimum total time the milking process must take.

输入描述

* Line 1: Two space-separated integers: N (the number of cows) and M
(the number of milking constraints; 1 <= M <= 50,000).

* Lines 2..1+N: Line i+1 contains the value of T(i) (1 <= T(i) <= 100,000).

* Lines 2+N..1+N+M: Each line contains two space-separated integers A
and B, indicating that cow A must be fully milked before one
can start milking cow B. These constraints will never form
a cycle, so a solution is always possible.

输出描述

* Line 1: The minimum amount of time required to milk all cows.

示例1

输入:

3 1
10
5
6
3 2

输出:

11

说明:

INPUT DETAILS:
There are 3 cows. The time required to milk each cow is 10, 5, and 6,
respectively. Cow 3 must be fully milked before we can start milking cow 2.

OUTPUT DETAILS:
Cows 1 and 3 can initially be milked at the same time. When cow 3 is
finished with milking, cow 2 can then begin. All cows are finished milking
after 11 units of time have elapsed.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

pypy3(pypy3.6.1) 解法, 执行用时: 1373ms, 内存消耗: 38340K, 提交时间: 2020-06-01 20:20:35

#!/usr/bin/env python3
#
# L. Milk Scheduling
#
import sys, os, math, re, time, random, copy, shutil, itertools, functools, heapq
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter, OrderedDict

def read_int(): return int(input())
def read_ints(): return list(map(int, input().split()))

################################################################################

n, m = read_ints()
t = [read_int() for _ in range(n)]
c = [0] * n
g = [[] for _ in range(n)]
for _ in range(m):
	u, v = read_ints()
	g[v - 1].append(u - 1)
	c[u - 1] += 1

vi = [-1] * n
q = deque()
for i in range(n):
	if c[i] == 0:
		q.append(i)
		vi[i] = t[i]
ans = 0
while q:
	u = q.popleft()
	ans = max(ans, vi[u])
	for v in g[u]:
		if vi[v] < vi[u] + t[v]:
			vi[v] = vi[u] + t[v]
			q.append(v)
print(ans)

C++11(clang++ 3.9) 解法, 执行用时: 44ms, 内存消耗: 1260K, 提交时间: 2020-05-31 11:38:39

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
int a[N],tot[N],in[N];
vector<int>v[N];
queue<int>q;
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		v[x].push_back(y);
		in[y]++;
	}
	for(int i=1;i<=n;i++)
	if(!in[i])
	{
		q.push(i);
		tot[i]=a[i];
	}
	int ans=-1;
	while(q.size())
	{
		int t=q.front();q.pop();
		for(int i=0;i<v[t].size();i++)
		{
			int u=v[t][i];
			if(--in[u]==0)q.push(u);
			tot[u]=max(tot[u],tot[t]+a[u]);
		}
	}
	for(int i=1;i<=n;i++)ans=max(ans,tot[i]);
	cout<<ans;
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 28ms, 内存消耗: 1376K, 提交时间: 2020-05-30 11:01:04

#include<cstdio>
#include<algorithm>
#include<vector>//头文件
using namespace std;
const int MAXN=10010;
vector<int> rule[MAXN];
int t[MAXN],f[MAXN],ans=0;
int find(int x){
	if(f[x]) return f[x];
	int maxt=0;
	for(int i=0;i<rule[x].size();i++){//这里改
		maxt=max(maxt,find(rule[x][i]));
	}
	return f[x]=maxt+t[x];
}
int main(){
	int n,m,a,b;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&t[i]);
	while(m--){
		scanf("%d%d",&a,&b);
		rule[a].push_back(b);//还有这里
	}
	for(int i=1;i<=n;i++){
		find(i);
		ans=max(ans,f[i]);
	}
	printf("%d",ans);
	return 0;
}

上一题