首页 > 其他 > 详细

【UVA1723】Intervals

时间:2019-07-31 18:20:13      阅读:79      评论:0      收藏:0      [点我收藏+]

题面

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("");
  }
}

 

【UVA1723】Intervals

原文:https://www.cnblogs.com/shxnb666/p/11277975.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!