hdu 1384 Intervals http://acm.hdu.edu.cn/showproblem.php?pid=1384 2013-2-14
f(b)-f(a)>=c
0<=f(x+1)-f(x)<=1
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <queue> 5 using namespace std; 6 7 const int N=50010; 8 struct BellmanFord{ 9 struct Edge{ 10 int v,w; 11 }; 12 int n; 13 vector<Edge> G[N]; 14 queue<int> q; 15 bool inq[N]; 16 int dis[N]; 17 int cnt[N]; 18 19 void init(int tn){ 20 n=tn; 21 for(int i=0;i<n;i++) G[i].clear(); 22 } 23 void addEdge(int u,int v,int w){ 24 G[u].push_back((Edge){v,w}); 25 } 26 bool negativeCycle(){ 27 while(!q.empty()) q.pop(); 28 memset(inq,0,sizeof(inq)); 29 memset(cnt,0,sizeof(cnt)); 30 for(int i=0;i<n;i++){ 31 dis[i]=0; 32 inq[i]=true; 33 q.push(i); 34 } 35 while(!q.empty()){ 36 int u=q.front(); q.pop(); 37 inq[u]=false; 38 int sz=G[u].size(); 39 for(int i=0;i<sz;i++){ 40 int v=G[u][i].v; 41 int w=G[u][i].w; 42 if(dis[v]>dis[u]+w){ 43 dis[v]=dis[u]+w; 44 if(!inq[v]){ 45 q.push(v); 46 inq[v]=true; 47 if(++cnt[v]>n) return true; 48 } 49 } 50 } 51 } 52 return false; 53 } 54 }; 55 56 BellmanFord g; 57 int a[N],b[N],c[N]; 58 int main(){ 59 int n; 60 while(~scanf("%d",&n)){ 61 int mx=0; 62 for(int i=0;i<n;i++){ 63 scanf("%d%d%d",&a[i],&b[i],&c[i]); 64 if(a[i]>mx) mx=a[i]; 65 if(b[i]>mx) mx=b[i]; 66 } 67 g.init(mx+2); 68 for(int i=0;i<n;i++){ 69 g.addEdge(b[i]+1,a[i],-c[i]); 70 } 71 for(int i=0;i<=mx;i++){ 72 g.addEdge(i,i+1,1); 73 g.addEdge(i+1,i,0); 74 } 75 g.negativeCycle(); 76 int ans=-g.dis[0]; 77 printf("%d\n",ans); 78 } 79 return 0; 80 }