poj 3680 intervals

来源:互联网 发布:热力学模拟软件 编辑:程序博客网 时间:2024/06/05 23:42

在费用流建模上完全是小白,还得练习。

这个题建模也挺难想,图是在数轴上建的,相邻数字之间连容量为无穷大的边,题目给的区间的数字之间再连一条容量为1的边,0到第一个数字之间连容量为k的边,这样,不重合的区间流量一定是1,有重合的区间流量要分别计算,在0到第一个数字的地方限定为k那么就不会有重合k次以上的区间,在这个图上求最大费用流即可。(事先离散化)

You are given N weighted open intervals. The ith interval covers (aibi) and weighs wi. Your task is to pick some of the intervals to maximize the total weights under the limit that no point in the real axis is covered more than k times.

#include<cstdio>#include<cstdlib>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<cstring>using namespace std;int n,k,num[500],mp[100005],c,size,par[500];bool v[500];#define INF ktypedef struct aa{int from,to,w,cap;friend bool operator < (aa a,aa b){if(a.from!=b.from) return a.from<b.from;if(a.to!=b.to) return a.to<b.to;}aa(int x,int y){from=x;to=y;w=0;cap=0;}aa(){}}Point;Point p[500];typedef struct ee{int from,to,w,cap;ee(int a,int b,int d,int c){from=a;to=b;cap=d;w=c;}ee(){}}Edge;Edge edge[5000];vector<int> G[500];int e=499;void add(int from,int to,int cap,int w){edge[size++]=Edge(from,to,cap,w);G[from].push_back(size-1);edge[size++]=Edge(to,from,0,-w);G[to].push_back(size-1);}void dscrt(){sort(num,num+2*n);c=unique(num,num+2*n)-num;for(int i=0;i<c;i++)mp[num[i]]=i+1;for(int i=1;i<=n;i++){p[i].from=mp[p[i].from];p[i].to=mp[p[i].to];}}void input(){scanf("%d%d",&n,&k);for(int i=0;i<n;i++){scanf("%d%d%d",&p[i+1].from,&p[i+1].to,&p[i+1].w);//p 点,从1到nnum[2*i]=p[i+1].from;num[2*i+1]=p[i+1].to;}dscrt();}void es_map(){add(0,1,k,0);for(int i=1;i<c;i++)add(i,i+1,INF,0);add(c,e,k,0);for(int i=1;i<=n;i++)add(p[i].from,p[i].to,1,-p[i].w);}bool bfs(){bool v[500]={0};int d[500];queue<int> Q;memset(par,-1,sizeof(par));Q.push(0);v[0]=1;d[0]=0;int now,next;for(int i=1;i<=499;i++)d[i]=INF;while(!Q.empty()){now=Q.front();Q.pop();v[now]=0;for(int i=0;i<G[now].size();i++){Edge u=edge[G[now][i]];if(u.cap<=0) continue;if(d[u.to]>d[now]+u.w){par[u.to]=G[now][i];d[u.to]=d[now]+u.w;if(!v[u.to]){Q.push(u.to);v[u.to]=1;}}}}return d[e]<=0;}int BF(){int ans=0;while(bfs()){int x=e;while(par[x]!=-1){ans+=edge[par[x]].w;edge[par[x]].cap--;edge[par[x]^1].cap++;x=edge[par[x]].from;}}return ans;}void init(){for(int i=0;i<=499;i++) G[i].clear();c=0;size=0;}int main(){int t;scanf("%d",&t);while(t--){init();input();es_map();printf("%d\n",-BF());}return 0;}

网络流部分差得很大,集训结束以后要着重加强。

0 0
原创粉丝点击