http://acm.hdu.edu.cn/showproblem.php?pid=1384
题目看了半天~~
思路:http://acm.miyuoo.com/2011/08/05/poj-1201hdu-1384-intervals-%E5%B7%AE%E5%88%86%E7%BA%A6%E6%9D%9F-spfa.html
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #define inf 0x3f3f3f3f using namespace std; const int MAXM=50005*4; const int MAXN=50005; struct node { int u,v,w; }; node edge[MAXM]; int first[MAXN],next[MAXM],cc; int inq[MAXN],dist[MAXN],cnt[MAXN]; inline void add_edge(int u,int v,int w) { edge[cc].u=u; edge[cc].v=v; edge[cc].w=w; next[cc]=first[u]; first[u]=cc; cc++; } bool SPFA(int s,int n) { int u,v,i,w; memset(inq,0,sizeof(inq)); memset(cnt,0,sizeof(cnt)); for(i=s;i<=n;i++) dist[i]=-inf; dist[s]=0; queue<int> q; q.push(s); while(!q.empty()) { u=q.front(); q.pop(); inq[u]=0; for(i=first[u];i!=-1;i=next[i]) { v=edge[i].v; w=edge[i].w; if(dist[v]<dist[u]+w) { dist[v]=dist[u]+w; if(!inq[v]) { cnt[v]++; if(cnt[v]>n) return false; inq[v]=1; q.push(v); } } } } return true; } int main() { int n; while(scanf("%d",&n)!=EOF) { int i,minn=inf,maxx=-inf; cc=0; memset(first,-1,sizeof(first)); memset(next,-1,sizeof(next)); for(i=0;i<n;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); v++; if(v>maxx) maxx=v; if(u<minn) minn=u; add_edge(u,v,w); } for(i=minn;i<maxx;i++) { add_edge(i,i+1,0); add_edge(i+1,i,-1); } SPFA(minn,maxx); printf("%d\n",dist[maxx]); } }