标签:前缀和 sep enter 建图 first standard print min 范围
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 26028 | Accepted: 9952 |
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
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef long long ll; const int N=5e4+5,M=15e4+5,INF=1e9; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x*f; } int n,m,a,b,c; struct edge{ int v,ne; double w; }e[M]; int h[N],cnt=0; inline void ins(int u,int v,int w){ cnt++; e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt; } int q[N],head,tail,inq[N],num[N],d[N]; inline void lop(int &x){if(x==N) x=1;else if(x==0) x=N-1;} bool spfa(){ head=tail=1; memset(inq,0,sizeof(inq)); memset(num,0,sizeof(num)); for(int i=0;i<=n;i++) q[tail++]=i,inq[i]=1,d[i]=0; while(head!=tail){ int u=q[head++];inq[u]=0;lop(head); for(int i=h[u];i;i=e[i].ne){ int v=e[i].v,w=e[i].w; if(d[v]<d[u]+w){ d[v]=d[u]+w; if(!inq[v]){ inq[v]=1; if(++num[v]>n) return true; if(d[v]>d[q[head]]) head--,lop(head),q[head]=v; else q[tail++]=v,lop(tail); } } } } return false; } int main(){ m=read(); for(int i=1;i<=m;i++){ a=read()+1;b=read()+1;c=read();n=max(n,b); ins(a-1,b,c); } for(int i=1;i<=n;i++) ins(i-1,i,0),ins(i,i-1,-1); spfa(); printf("%d",d[n]); }
标签:前缀和 sep enter 建图 first standard print min 范围
原文地址:http://www.cnblogs.com/candy99/p/6192851.html