Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class FVol3 {
- public static void main(String[] args) {
- Scanner stdin = new Scanner(System.in);
- int n = stdin.nextInt();
- int t = stdin.nextInt();
- Grafo graph = new Grafo(n);
- int[] prec = new int[n + 1];
- for (int i = 0; i < t; i++) {
- int from = stdin.nextInt();
- int to = stdin.nextInt();
- int val = stdin.nextInt();
- graph.insert_new_arc(from, to, val);
- prec[to]++;
- }
- int[] es = new int[n + 1];
- int[] starts = new int[t];
- int[] ends = new int[t];
- Queue<Integer> queue = new LinkedList<>();
- queue.add(1);
- int tnum = 0;
- while (!queue.isEmpty()) {
- int node = queue.remove();
- for (Arco task : graph.adjs_no(node)) {
- int end = task.extremo_final();
- int duration = task.valor_arco();
- if (es[end] < es[node] + duration)
- es[end] = es[node] + duration;
- starts[tnum] = es[node];
- ends[tnum] = es[node] + duration;
- tnum++;
- prec[end]--;
- if (prec[end] == 0)
- queue.add(end);
- }
- }
- Arrays.sort(starts);
- Arrays.sort(ends);
- int currentTasks = 1;
- int maxTasks = 1;
- int time = starts[0];
- int i = 1, j = 0;
- while (i < t && j < t) {
- if (starts[i] < ends[j]) {
- currentTasks++;
- if (currentTasks > maxTasks) {
- maxTasks = currentTasks;
- time = starts[i];
- }
- i++;
- } else {
- currentTasks--;
- j++;
- }
- }
- System.out.println(es[n] + " " + maxTasks + " " + time);
- }
- }
- class Arco {
- int no_final;
- int valor;
- Arco(int fim, int v) {
- no_final = fim;
- valor = v;
- }
- int extremo_final() {
- return no_final;
- }
- int valor_arco() {
- return valor;
- }
- void novo_valor(int v) {
- valor = v;
- }
- }
- class No {
- // int label;
- LinkedList<Arco> adjs;
- No() {
- adjs = new LinkedList<Arco>();
- }
- }
- class Grafo {
- No verts[];
- int nvs, narcos;
- public Grafo(int n) {
- nvs = n;
- narcos = 0;
- verts = new No[n + 1];
- for (int i = 0; i <= n; i++)
- verts[i] = new No();
- // para vertices numerados de 1 a n (posicao 0 nao vai ser usada)
- }
- public int num_vertices() {
- return nvs;
- }
- public int num_arcos() {
- return narcos;
- }
- public LinkedList<Arco> adjs_no(int i) {
- return verts[i].adjs;
- }
- public void insert_new_arc(int i, int j, int valor_ij) {
- verts[i].adjs.addFirst(new Arco(j, valor_ij));
- narcos++;
- }
- public Arco find_arc(int i, int j) {
- for (Arco adj : adjs_no(i))
- if (adj.extremo_final() == j)
- return adj;
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement