【UVA1723】Intervals

题面

https://www.luogu.org/problem/UVA1723

题解

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#define ri register int
#define N 50500
using namespace std;

int T,n;
int d[N];
vector<int> to[N],len[N];
bool inq[N];

void add_edge(int a,int b,int c) {
  to[a].push_back(b); 
  len[a].push_back(c);
}

void spfa() {
  memset(d,-1,sizeof(d));
  memset(inq,0,sizeof(inq));
  d[0]=0;
  queue<int> q;
  q.push(0); inq[0]=1;
  while (!q.empty()) {
    int x=q.front(); q.pop(); inq[x]=0;
    for (ri i=0;i<to[x].size();i++) {
      int y=to[x][i];
      if (d[y]<d[x]+len[x][i]) {
        d[y]=d[x]+len[x][i];
        if (!inq[y]) {
          inq[y]=1;
          q.push(y);
        }
      }
    }
  }
}

int main(){
  int a,b,c;
  scanf("%d",&T);
  while (T--)  {
    scanf("%d",&n);
    for (ri i=0;i<N;i++) to[i].clear(),len[i].clear();
    int maxb=0;
    for (ri i=1;i<=n;i++) {
      scanf("%d %d %d",&a,&b,&c);
      a++; b++;
      if (b>maxb) maxb=b;
      add_edge(a-1,b,c);
    }
    for (ri i=0;i<N-1;i++) {
      add_edge(i,i+1,0);
      add_edge(i+1,i,-1);
    }
    spfa();
    if (!T) cout<<d[N-1]; else cout<<d[N-1]<<endl;
    puts("");
  }
}
原文地址:https://www.cnblogs.com/shxnb666/p/11277975.html