HDU 1534(Schedule Problem)

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;

struct Edge
{
    int to, cost, next;
}edge[MAXN * MAXN];

int n; //任务数
int time[MAXN]; //完成任务所需时间
int total, head[MAXN];
int dis[MAXN], vis[MAXN], cnt[MAXN];

void addEdge(int now, int v, int w)
{
    edge[total].to = v;
    edge[total].cost = w;
    edge[total].next = head[now];
    head[now] = total++;
}

bool SPFA()
{
    for (int i = 0; i < MAXN; i++)
    {
        vis[i] = 0;
        cnt[i] = 0;
        dis[i] = -INF;
    }
    queue<int> q;
    dis[0] = 0;
    vis[0] = 1;
    cnt[0] = 1;
    q.push(0);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        vis[now] = 0;
        for (int i = head[now]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to, w = edge[i].cost;
            if (dis[v] < dis[now] + w)
            {
                dis[v] = dis[now] + w;
                if (!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                    cnt[v]++;
                    if (cnt[v] > n)
                        return false;
                }
            }
        }
    }
    return true;
}

int main()
{
    int Case = 0;
    while (cin >> n)
    {
        if (n == 0)
            break;

        for (int i = 1; i <= n; i++)
            cin >> time[i];

        total = 0;
        memset(head, -1, sizeof(head));
        string s;
        int a, b;
        while (cin >> s)
        {
            if (s == "#")
                break;
            cin >> a >> b;
            if (s == "FAS")
                addEdge(b, a, -time[a]);
            else if (s == "FAF")
                addEdge(b, a, time[b] - time[a]);
            else if (s == "SAF")
                addEdge(b, a, time[b]);
            else if (s == "SAS")
                addEdge(b, a, 0);
        }

        for (int i = 1; i <= n; i++)
            addEdge(0, i, 0);

        cout << "Case " << ++Case << ":" << endl;
        if (SPFA())
        {
            for (int i = 1; i <= n; i++)
                cout << i << " " << dis[i] << endl;
        }
        else
            cout << "impossible" << endl;
        cout << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值