标签:style blog color os io ar for 2014 div
5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
6
题意:给你n个数u,v,w;要求在[u,v]区间至少取w个数(整数),求最少要取多少个数。
S[v+1] - S[u] >= w, S[i+1] - S[i] >=0&&<=1,S[i] - S[i+1] <=-1.
在u,v+1之间建一条边,跑一遍SPFA即可。
#include <iostream> #include <stdio.h> #include <string.h> #include<string> #include<math.h> #include<queue> #include<stack> #include <algorithm> #include<vector> #include<map> using namespace std; #define M 100 #define inf 0x3fffffff #define maxn 50005 #define ll long long struct edge { int v,num,next; }e[maxn*4]; int edge_num,a,b,vis[maxn],d[maxn]; int head[maxn]; void add(int a,int b,int c) { e[edge_num].num=b; e[edge_num].v=c; e[edge_num].next=head[a]; head[a]=edge_num++; } int n; void SPFA() { queue<int>q; memset(vis,0,sizeof(vis)); memset(d,-50005,sizeof(d)); /*for(int i=a;i<=b;i++) d[i]=-50005;*/ while(!q.empty()) q.pop(); q.push(a); d[a]=0;vis[a]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].num; if(d[v]<d[u]+e[i].v) { d[v]=d[u]+e[i].v; if(!vis[v]) { q.push(v); vis[v]=1; } } } vis[u]=0; } } int main() { while(~scanf("%d",&n)) { edge_num=0; memset(head,-1,sizeof(head)); int u,v,w; a=inf,b=0; while(n--) { scanf("%d%d%d",&u,&v,&w); if(u<a) a=u; if(v+1>b) b=v+1; add(u,v+1,w); } for(int i=a;i<b;i++) { add(i+1,i,-1); add(i,i+1,0); } SPFA(); printf("%d\n",d[b]); } return 0; }
标签:style blog color os io ar for 2014 div
原文地址:http://blog.csdn.net/u012861385/article/details/39033617