栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1508 Intervals

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1508 Intervals

#include <cstdio>#include <cstring>#include <iostream>#include <stack>using namespace std;const int INF=0x3fffffff;const int N=50006;const int E=160006;struct Edge{    int st, en, val, next;    Edge() {}    Edge(int a, int b, int c, int d)    {        st=a, en=b, val=c, next=d;    }} edge[E];int head[N], tot;int cnt[N], dis[N];bool vs[N];stack<int> S;void add_edge(int st, int en, int val){    edge[tot]=Edge(st, en, val, head[st]);    head[st]=tot++;}int SPFA(int s, int t, int n){    for(int i=0; i<=n; i++) vs[i]=false, dis[i]=-INF, cnt[i]=0;    while(!S.empty()) S.pop();    S.push(s), vs[s]=true, dis[s]=0, cnt[s]++;    while(!S.empty())    {        int u=S.top();        S.pop(), vs[u]=false;        if(cnt[u]>n) return -1;        for(int e=head[u]; e!=-1; e=edge[e].next)        { int v=edge[e].en; int val=edge[e].val; if(dis[v]<dis[u]+val) {     dis[v]=dis[u]+val;     if(!vs[v]) vs[v]=true, S.push(v), cnt[v]++; }        }    }    return dis[t];}int main(){    int n;    while(scanf("%d", &n)!=EOF)    {        memset(head, -1, sizeof head);        tot=0;        int Max=0;        for(int i=0, a, b, c; i<n; i++)        { scanf("%d%d%d", &a, &b, &c); Max=max(Max, b); add_edge(a, b+1, c);        }        for(int i=1; i<=Max+1; i++) add_edge(i, i-1, -1), add_edge(i-1, i, 0);        int ans=SPFA(1, Max+1, Max+1);        printf("%dn", ans);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372971.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号