求助
查看原帖
求助
338147
01bit楼主2021/3/14 10:25
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
int read()
{
    int x = 0, f = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar())
        f = c == '-' ? -1 : 1;
    for (; isdigit(c); c = getchar())
        x = x * 10 + c - '0';
    return x * f;
}
struct star
{
    int v, w;
    int next = 0;
} edge[10001];
int cnt = 1, head[101];
void addedge(int u, int v, int w)
{
    edge[cnt].v = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
int in[101];
bool book[101];
int n, p;
int u[101], c[101];
int out[101], top = 0;
void toposort()
{
    bool flag;
    int cnt = 0;
    do
    {
        flag = 0;
        for (int x = 1; x <= n; x++)
            if (!in[x] && !book[x])
            {
                if (!cnt)
                    c[x] -= u[x];
                flag = 1;
                book[x] = 1;
                if (!head[x])
                    out[top++] = x;
                for (int i = head[x]; i; i = edge[i].next)
                {
                    int v = edge[i].v, w = edge[i].w;
                    in[v]--;
                    c[v] += w * c[x];
                }
            }
        cnt++;
    } while (flag);
}
int main()
{
    n = read(), p = read();
    for (int i = 1; i <= n; i++)
    {
        c[i] = read();
        u[i] = read();
    }
    for (int i = 1; i <= p; i++)
    {
        int x = read(), v = read(), w = read();
        addedge(x, v, w);
        in[v]++;
    }
    toposort();
    sort(out, out + top);
    int cnt = 0;
    for (int i = 0; i < top; i++)
    {
        int x = out[i];
        if (c[x] > 0)
            printf("%d %d\n", x, c[x]);
        cnt += c[x] > 0;
    }
    if (!cnt)
        puts("NULL");
    return 0;
}
2021/3/14 10:25
加载中...