POJ

来源:互联网 发布:淘宝怎么修改旺旺名称 编辑:程序博客网 时间:2024/06/08 02:04

传送门:POJ3762

题意:每天都有n种工作,每种工作有不同的价值,每种工作只能做一次,某一时刻只能做一种工作,问k天内最多可获得的价值是多少。

思路:这题就是POJ3680的翻版,做法详见POJ3680。

代码:

#include<stdio.h>#include<iostream>#include<queue>#include<string.h>#include<algorithm>#define ll long long#define inf 0x3f3f3f3fusing namespace std;const int MAXN=4010;const int MAXM=1000010;int head[MAXN],pre[MAXN],dis[MAXN],book[MAXN];int cnt=0,N;//N为点数 struct node{int to,next,cap,flow,cost;}edge[MAXM];void init(){cnt=0;memset(head,-1,sizeof(head));}void addedge(int u,int v,int cap,int cost){edge[cnt].to=v;edge[cnt].cap=cap;edge[cnt].flow=0;edge[cnt].cost=cost;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].cap=0;edge[cnt].flow=0;edge[cnt].cost=-cost;edge[cnt].next=head[v];head[v]=cnt++;}bool spfa(int s,int t){queue<int>q;while(!q.empty())q.pop();for(int i=0;i<=N;i++){dis[i]=inf;pre[i]=-1;book[i]=0;}dis[s]=0;book[s]=1;q.push(s);while(!q.empty()){int u=q.front();q.pop();book[u]=0;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost){dis[v]=dis[u]+edge[i].cost;pre[v]=i;if(!book[v]){book[v]=1;q.push(v);}} } }return pre[t]!=-1;}int min_cost_max_flow(int s,int t,int &cost){int flow=0;cost=0;while(spfa(s,t)){int temp=inf;for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])temp=min(temp,edge[i].cap-edge[i].flow);for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){edge[i].flow+=temp;edge[i^1].flow-=temp;cost+=edge[i].cost*temp;}flow+=temp;}return flow;}int W[MAXN],L[MAXN],R[MAXN];vector<int>p;int main(){//string s="002";//cout<<atoi(s.c_str());ios::sync_with_stdio(false);int n,k;init();cin>>n>>k;string l,r;for(int i=1;i<=n;i++){cin>>l>>r>>W[i];L[i]=atoi(l.substr(0,2).c_str())*3600+atoi(l.substr(3,2).c_str())*60+atoi(l.substr(6,2).c_str());R[i]=atoi(r.substr(0,2).c_str())*3600+atoi(r.substr(3,2).c_str())*60+atoi(r.substr(6,2).c_str());p.push_back(L[i]);p.push_back(R[i]);}sort(p.begin(),p.end());p.erase(unique(p.begin(),p.end()),p.end());int source=0,sink=p.size()+1;N=sink;addedge(0,1,k,0);addedge(sink-1,sink,k,0);for(int i=1;i<p.size();i++)addedge(i,i+1,inf,0);for(int i=1;i<=n;i++){int u=lower_bound(p.begin(),p.end(),L[i])-p.begin()+1;int v=lower_bound(p.begin(),p.end(),R[i])-p.begin()+1;addedge(u,v,1,-W[i]);}int cost;min_cost_max_flow(source,sink,cost);cout<<-cost<<endl;}


原创粉丝点击