Sicily 1308. Dependencies among J

来源:互联网 发布:网络老虎机熊猫机器 编辑:程序博客网 时间:2024/05/21 07:08

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

Problem Source

ZSUACM Team Member

// Problem#: 1308// Submission#: 3279120// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <iostream>#include <vector>#include <string>#include <stack>#include <iomanip>#include <algorithm>#include <queue>#include <functional>using namespace std;const int MAX_V = 10005;const int INF = 99999999;int N, M;vector<int> G[MAX_V];int cost[MAX_V];char in[MAX_V * 1000];void read() {    for (int i = 1; i <= N; i++) {        G[i].clear();    }    for (int i = 1; i <= N; i++) {        gets(in);        int j = 0;        int c = 0;        int t = 0;        for (; in[j] != ' ' && in[j] != '\0'; j++) {            c = c * 10 + in[j] - '0';        }        cost[i] = c;        if (in[j] == '\0') continue;        for (j++; 1; j++) {            if (in[j] == ' ') {                G[i].push_back(t);                t = 0;            } else if (in[j] == '\0') {                G[i].push_back(t);                break;            } else {                t = t * 10 + in[j] - '0';            }        }        }}bool haveTo[MAX_V];bool vis[MAX_V];void searchHaveTo(int from) {    vis[from] = haveTo[from] = true;    int s = G[from].size();    for (int i = 0; i < s; i++) {        if (!vis[G[from][i]]) {            true;            searchHaveTo(G[from][i]);        }    }}int findAns() {    for (int i = 1; i <= N; i++) haveTo[i] = vis[i] = false;    haveTo[M] = vis[M] = true;    int s = G[M].size();    for (int i = 0; i < s; i++) {        if (!vis[G[M][i]]) {            searchHaveTo(G[M][i]);        }    }    int sum = 0;    for (int i = 1; i <= N; i++) {        if (haveTo[i]) sum += cost[i];    }    return sum;}int main() {    //std::ios::sync_with_stdio(false);    while (1) {        scanf("%d", &N);        if (N == 0) break;        scanf("%d\n", &M);        read();        printf("%d\n", findAns());    }    return 0;}                                 


0 0
原创粉丝点击