UVA間隔

UVA間隔

説明

  • 互いに異なるCIは任意の整数をとり、少なくとも区間[AI、BI]において、n個のセクションを有しています。少なくとも正の整数を取るために、n個のセクションのケースを満たすために求めています。

入力

  • 複数のデータセット。

    最初の整数、Tは空白行に続くデータラインの数を表します。

    各テストの場合:

    最初の行は、区間数を表す整数nを(1 <= N <= 50000)を含みます。

    以下のセクションでは、行nを記載します。

    最初の入力(I + 1)行は三つの整数スペースで区切られたAI、BI、CIを含んでいます。0 <= aiを<= BI <= 50000,1 <= CI <= BI-AI + 1。

出力

  • 各試験のために、出力は、n個の区間[AI、BI]の合計数CI異なる整数の少なくとも数をとります。

    空白行のデータ出力の最後のセットに加えて。

サンプル入力

1

5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

サンプル出力

6

ソリューション:

  • 微分制約。
  • 作業が終了したら羅区P1250の木は、この問題を行う、あなたが叫びます:「ああ、元のタイトル!」
  • 実際には、本質は同じです。
  • だから、問題を解決するには、電源を入れBenpian エッセイを
  • また、なぜこのような単純な疑問は、紫...です...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 200005
using namespace std;

struct E {int next, to, dis;} e[N];
int T, m, num, n;
int h[N], dis[N];
bool vis[N];

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

void add(int u, int v, int w)
{
    e[++num].next = h[u];
    e[num].to = v;
    e[num].dis = w;
    h[u] = num;
}

void spfa()
{
    queue<int> que;
    memset(dis, -0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    dis[n + 1] = 0, vis[n + 1] = 1, que.push(n + 1);
    while(!que.empty())
    {
        int now = que.front();
        que.pop(); vis[now] = 0;
        for(int i = h[now]; i != 0; i = e[i].next)
            if(dis[now] + e[i].dis > dis[e[i].to])
            {
                dis[e[i].to] = dis[now] + e[i].dis;
                if(!vis[e[i].to])
                    vis[e[i].to] = 1, que.push(e[i].to);
            }
    }
}

int main()
{
    cin >> T;
    for(int dfn = 1; dfn <= T; dfn++)
    {
        n = num = 0;
        memset(h, 0, sizeof(h));
        m = read();
        for(int i = 1; i <= m; i++)
        {
            int a = read(), b = read(), c = read();
            add(a - 1, b, c), n = max(n, b);
        }
        for(int i = 1; i <= n; i++) add(i - 1, i, 0), add(i, i - 1, -1);
        for(int i = 0; i <= n; i++) add(n + 1, i, 0);
        spfa();
        printf("%d\n", dis[n]);
        if(dfn != T) printf("\n");
    }
    return 0;
}

おすすめ

転載: www.cnblogs.com/BigYellowDog/p/11232079.html