uva 10461 Difference

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 44 3 2 12 12 43 1 3 421 2 4 44 3 2 12 12 43 1 3 423 40 0

Sample Output

Case #1:12Case #2:34

Author : Mohammad Sajjad Hossain

The Real Programmers' Contest-2


虽然是一道水题,还是要写解题报告吐槽一下。题中明明说所有任务都是可以完成的,为什么会出现环的情况。害我dfs没加vis判断超时到死,简直了。。。再吐槽今天多校,比赛没过的2题,赛后立马A了,这真是状态呀。。。

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<cmath>
using namespace std;
const int maxn = 500 + 5;
const int INF = 1000000000;
const double eps = 1e-6;
typedef unsigned long long ULL;
typedef pair<int, int> P;
#define fi first
#define se second

vector<int> G[maxn];
int t[maxn];
int can[maxn][maxn];
int vis[maxn];

void dfs(int source, int x){
    can[source][x] = 1;
    vis[x] = 1;
    for(int i = 0;i < G[x].size();i++){
        int to = G[x][i];
        if(!vis[to])
            dfs(source, to);
    }
}

int main(){
    int n, m;
    int kase = 0;
    while(scanf("%d%d", &n, &m)){
        kase++;
        if(n == 0 && m == 0)
            break;
        for(int i = 1;i <= n;i++)
            scanf("%d", &t[i]);
        memset(can, 0, sizeof can);
        for(int i = 1;i <= n;i++)
            G[i].clear();
        while(m--){
            int x, y;
            scanf("%d%d", &x, &y);
            G[y].push_back(x);
        }
        for(int i = 1;i <= n;i++){
            memset(vis, 0, sizeof vis);
            dfs(i, i);
        }

        printf("Case #%d:\n", kase);
        int q;
        scanf("%d", &q);
        while(q--){
            int x;
            scanf("%d", &x);
            int ans = 0;
            for(int i = 1;i <= n;i++){
                if(i != x){
                    if(can[x][i]!=1 && can[i][x]!=1)
                        ans += t[i];
                }
            }
            printf("%d\n", ans);
        }
        puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值