HDU 1384(Intervals)

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

struct Edge //边
{
    int node, len;
    Edge* next;
}edge[MAXN * 10];

Edge* head[MAXN];
int totalE; //总边数
int vis[MAXN], dist[MAXN];

//增加边
void addEdge(int a, int b, int c)
{
    edge[totalE].node = b;
    edge[totalE].len = c;
    edge[totalE].next = head[a];
    head[a] = edge + totalE++;
}

//SPFA算法
void SPFA(int n)
{
    queue<int> q;
    vis[n] = 1;
    dist[n] = 0;
    q.push(n);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        vis[now] = 0;
        for (Edge* p = head[now]; p != NULL; p = p->next)
        {
            int next = p->node;
            if (dist[next] < dist[now] + p->len)
            {
                dist[next] = dist[now] + p->len;
                if (!vis[next])
                {
                    vis[next] = 1;
                    q.push(next);
                }
            }
        }
    }
}

int main()
{
    int n;
    while (cin >> n)
    {
        totalE = 0;
        fill(head, head + MAXN, (Edge*)NULL);
        fill(dist, dist + MAXN, -INF);
        memset(vis, 0, sizeof(vis));

        int a, b, c;
        int maxR = 0; //最右结点
        for (int i = 0; i < n; i++)
        {
            cin >> a >> b >> c;
            maxR = max(maxR, b);
            addEdge(a - 1, b, c);
        }

        for (int i = 1; i <= maxR; i++)
        {
            addEdge(i - 1, i, 0);
            addEdge(i, i - 1, -1);
        }
        SPFA(0);
        cout << dist[maxR] << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值