24h購物| | PChome| 登入
2014-01-24 09:31:30| 人氣2,939| 回應0 | 上一篇 | 下一篇

[UVA] 10461 - Difference

推薦 0 收藏 0 轉貼0 訂閱站台

Problem I

Difference

Input: standard input

Output: standard output

Time Limit: 6 seconds

 

You are given a list of jobs with associated time to complete them. Also are given a list of job dependencies. Your job is to calculate the maximum possible difference among all possible completion time of a particular job. For this problem, job dependency is a bit simpler. When a job depends on several other jobs to be completed, finishing of all of them will allow it to execute. Furthermore, jobs cannot be executed in parallel and there will be no idle moment.

In the above example job-B cannot be started before A and D. So it can be started after 5 days and it must be started after 7 days. Here for job-B, the difference is 2 days.

 

Input

Input starts with a pair of integers v(1<=v<=500) and e(0<=e<=500) where v represents the number of jobs and e represents the number of dependencies in the dependency list. Following is a line with v integers each indicating the time in days to complete the jobs where the i'th integer denotes the completion time of the i'th job. The jobs are numbered by integers from 1 to v. Next e lines has the form "x y" which means that job x should be completed prior to job y(1 <= x, y <= v). Next line has an integer q which denotes the number of queries to answer which is followed by q integers x (1 <= x <= v). A line with v = e = 0 ends the input session. Every block will be followed by a blank line. There will be no impractical situation (Each job can be completed) in input.


Output

For each query in a block output the result according to the problem description in a separate line. A blank line is essential after every block of data. See the sample output.

 

Sample Input

4 4 4 3 2 1 2 1 2 4 3 1 3 4 2 1 2 4 4 4 3 2 1 2 1 2 4 3 1 3 4 2 3 4 0 0

Sample Output

Case #1: 1 2 Case #2: 3 4

Author : Mohammad Sajjad Hossain

The Real Programmers' Contest-2

題目描述:


工作之間有依靠關係,當什麼完成後才能完成什麼工作。

求完成某個工作的最晚時間和最早時間的差值。

題目解法:

給定的圖一定是 DAG,給一個工作 x,對於 x 找到所有依靠關係的工作(往 x~>y),
往下搜索,由於工作不能平行,把所有的相關工作天數相加即可,便可得到最早時間。
對給定的圖,直接著手 BFS 或者是 DFS 即可。

最晚時間則是附加一些毫無依靠關係的工作,也就是除了 w~>x 的相關工作 (摒除所有 w)。
為了方便計算,使用扣除的方式,因此關係是相反的,要存一個反向邊的圖進行 DFS 或者是 BFS。

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
vector<int> g[505], vg[505];
int d[505], used[505];
int dfs(int v) {
    if(used[v])    return 0;
    used[v] = 1;
    int sum = d[v];
    for(int i = 0; i < g[v].size(); i++)
        sum += dfs(g[v][i]);
    return sum;
}
int dfs2(int v) {
    if(used[v])    return 0;
    used[v] = 1;
    int sum = d[v];
    for(int i = 0; i < vg[v].size(); i++)
        sum += dfs2(vg[v][i]);
    return sum;
}
int main() {
    int cases = 0;
    int n, m, q;
    int i, j, k, x, y;
    while(scanf("%d %d", &n, &m) == 2 && n) {
        int totW = 0;
        for(i = 1; i <= n; i++) {
            scanf("%d", &d[i]);
            totW += d[i];
        }
        for(i = 1; i <= n; i++)
            g[i].clear(), vg[i].clear();
        for(i = 0; i < m; i++) {
            scanf("%d %d", &x, &y);
            g[x].push_back(y);
            vg[y].push_back(x);
        }
        scanf("%d", &q);
        printf("Case #%d:\n", ++cases);
        while(q--) {
            scanf("%d", &x);
            memset(used, 0, sizeof(used));
            int minD = dfs(x);
            memset(used, 0, sizeof(used));
            int maxD = totW - dfs2(x) + d[x];
            printf("%d\n", maxD - minD);
        }
        puts("");
    }
    return 0;
}
/*
4 4
4 3 2 1
2 1
2 4
3 1
3 4
2
1 2

4 4
4 3 2 1
2 1
2 4
3 1
3 4
2
3 4

0 0
*/

台長: Morris
人氣(2,939) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][dp] 672 - Gangsters
此分類上一篇:[UVA][二分] 12715 - Watching the Kangaroo

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文