标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25125 | Accepted: 9580 |
Description
Input
Output
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Source
差分约束
1 /**/ 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstring> 6 #include<algorithm> 7 #include<queue> 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 const int mxn=60000; 11 struct edge{ 12 int v,dis; 13 int next; 14 }e[mxn*10]; 15 int hd[mxn],cnt; 16 int dis[mxn]; 17 int vis[mxn]; 18 int inq[mxn]; 19 int n; 20 void add_edge(int u,int v,int w){ 21 e[++cnt].next=hd[u];e[cnt].v=v;e[cnt].dis=w;hd[u]=cnt; 22 } 23 bool SPFA(){ 24 memset(dis,-1,sizeof dis); 25 queue<int>q; 26 q.push(0); 27 inq[0]=1; 28 vis[0]=1; 29 dis[0]=0; 30 while(!q.empty()){ 31 int u=q.front(); 32 for(int i=hd[u];i;i=e[i].next){ 33 int v=e[i].v; 34 if(dis[u]+e[i].dis>dis[v]){ 35 dis[v]=dis[u]+e[i].dis; 36 vis[v]++; 37 if(vis[v]>n)return 0; 38 if(!inq[v]){ 39 inq[v]=1; 40 q.push(v); 41 } 42 } 43 } 44 q.pop(); 45 inq[u]=0; 46 } 47 return 1; 48 } 49 int main(){ 50 scanf("%d",&n); 51 int i,j; 52 int mx=-1,a,b,c; 53 for(i=1;i<=n;i++){ 54 scanf("%d%d%d",&a,&b,&c); 55 add_edge(a,b+1,c); 56 mx=max(mx,b+1); 57 } 58 for(i=0;i<mx;i++){ 59 add_edge(i,i+1,0); 60 add_edge(i+1,i,-1); 61 } 62 SPFA(); 63 printf("%d\n",dis[mx]); 64 return 0; 65 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5742599.html