sicily 1308. Dependencies among J

1308. Dependencies among J

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

As everybody knows, our staffs need to do a lot of jobs to prepare for GDCPC’2003 (ZSUCPC’2003). But I bet you can not image how terrible to arrange the jobs. We know, sometimes there are dependencies among the jobs. We say job 2 depends on job 1 that means before starting job 1 we must finish job 2. We assume that there is only one job processing in one moment, and any job is dependent on no more than ten jobs. 

 

When we make up a jobs’ schedule, we should check whether it is valid. Now, your task is to find out the earliest finish time of some jobs.

Input

Input will contain several test cases. The first line of each test case contains two integer numbers N (0≤N≤10,000) and M. The jobs are numbered from 1 to N. You need to calculate the earliest finish time of the job M. And then, the following N lines describe jobs. The first line is corresponding the job 1, second line is corresponding the job 2 and so on. 

 

Each job’s describing line contains several positive integer numbers. The numbers are separated by spaces. The first one of the ith line shows the time (≤100) that ith job cost. The rest of numbers of the ith line are the jobs on which the job I depends. 

 

N=0 indicate the end of input file. We guaranteed there is no circle on dependency.

Output

For each test case you should output one line, and just one number in this line. The number is the earliest finishing time of job M.

Sample Input

2 232 13 332 14 1 20

Sample Output

59

题目分析

已知各个任务的花费时间和依赖关系
求执行到某个任务最快需要多久
有向图,以该任务为根,反溯直到根节点(可能有多个)
注意可能某个点会回溯几次,要打好标记避免重复计算,防止超时


#include <iostream>
#include <vector>

struct Node {
  int cost;
  int papa[11];
  int count;
} nodes[10001];
int num, target;
int ans;

void deal(std::string line, int index) {
  int temp = 0;
  int i;
  for (i = 0; line[i] != ' ' && i < line.length(); ++i)
    temp = temp * 10 + (line[i]-'0');
  nodes[index].cost = temp;
  temp = 0;
  for (i = i + 1; i < line.length(); ++i) {
    if (line[i] == ' ') {
      nodes[index].papa[nodes[index].count++] = temp;
      temp = 0;
    } else {
      temp = temp * 10 + (line[i]-'0');
    }
  }
  if (temp != 0)
    nodes[index].papa[nodes[index].count++] = temp;
}

void dfs(int index) {
  ans += nodes[index].cost;
  nodes[index].cost = 0;
  for (int i = 0; i < nodes[index].count; ++i) {
    if (nodes[nodes[index].papa[i]].cost != 0)
      dfs(nodes[index].papa[i]);
  }
}


int main()
{
  while (std::cin >> num && num != 0) {
    std::cin >> target;
    std::string line;
    getline(std::cin, line);
    for (int i = 0; i <= num; ++i)
      nodes[i].count = 0;
    for (int i = 1; i <= num; ++i) {
      getline(std::cin, line);
      deal(line, i);
    }
    ans = 0;
    dfs(target);
    std::cout << ans << std::endl;
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值