POJ 1201 差分约束

来源:互联网 发布:js缺少对象,怎样调试 编辑:程序博客网 时间:2024/05/16 17:29
////  main.cpp//  POJ 1201 差分约束////  Created by 郑喆君 on 8/8/14.//  Copyright (c) 2014 itcast. All rights reserved.////取最大值的时候差分方程形式为a-b<=c 求图的最短距离。取最小值的时候查分方程的形式为a-b>=c,求图的最长距离#include<cstdio>#include<cstring>#include<iostream>#include<iomanip>#include<queue>#include<cmath>#include<stack>#include<map>#include<vector>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int int_max = 0x07777777;const int int_min = 0x80000000;const int maxn = 50005;//struct Edge {//    int from, to, dist;//    Edge(int _from, int _to, int _dist):from(_from),to(_to),dist(_dist){}//};struct Edge {    int to, dist,next;}es[maxn*3];int head[maxn],d[maxn],vis[maxn],cnt;void addedge (int from, int to, int dist){    es[cnt].to = to;    es[cnt].dist = dist;    es[cnt].next = head[from];    head[from] = cnt++;}int n,s,t;void spfa (){    for(int i = s; i <= t; i++) d[i] = int_min;    memset(vis, 0, sizeof(vis));    d[s] = 0;    queue<int> q;    q.push(s);    vis[s] = 1;    while(!q.empty()){        int u = q.front();        q.pop();        vis[u] = 0;        for(int i = head[u]; i != -1; i = es[i].next){            if(d[es[i].to] < d[u]+es[i].dist){                d[es[i].to] = d[u]+es[i].dist;                if(!vis[es[i].to]){                    q.push(es[i].to);                    vis[es[i].to] = 1;                }            }        }    }}int main(int argc, const char * argv[]){    while(scanf("%d", &n)!=EOF){        cnt = 0;        s = int_max;        t = int_min;        memset(head, -1, sizeof(head));        for(int i = 0; i < n; i++){            int x,y,z;            scanf("%d %d %d", &x, &y, &z);            s = (s > x ? x : s);            t = (t < y+1 ? y+1 : t);            addedge(x, y+1, z);        }        for(int i = s; i < t; i++) {            addedge(i, i+1, 0);            addedge(i+1, i, -1);        }        spfa();        printf("%d\n", d[t]);    }}

0 0