Hdu 4494 Teamwork(最小费用流)
来源:互联网 发布:sql truncate 函数 编辑:程序博客网 时间:2024/05/22 18:31
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4494
思路:每种属性人互不干扰,跑m次费用流,结果累加。超级源点0,超级汇点2*n-1。将每个点拆成两个点一个为自己,另一个表示可以提供给别人。源点向每个表示自己的点连一条容量为INF,费用为1的边,表示起点有无数人每选择一人需花费1。对于每个拆出来的点,源点向其连一条容量为kind[i](第i中需要人数),费用为0的边。表示当前这个点最多可以提供kind[i]的人,费用为0。对于第i个任务,若worker[i].b+worker[i].p+dist<=worker[j].b,则将i拆出来点向表示j自己的点连边,表示可以提供worker。
#include<cstdio>#include<queue>#include<cmath>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#define debuusing namespace std;const int maxn=500;const int INF=0x3f3f3f3f;typedef long long LL;struct Edge{ int from,to,cap,flow,cost; Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w) {}};struct Node{ int kind[6]; int b,p,x,y;};int s,t,stx,sty;Node worker[maxn];vector<Edge> edges;vector<int> G[maxn];int n,m,p[maxn],a[maxn];int inq[maxn],d[maxn];LL cost;void init(int n){ edges.clear(); for(int i=0; i<=n; i++) G[i].clear();}void addedges(int from,int to,int cap,int cost){ edges.push_back(Edge(from,to,cap,0,cost)); edges.push_back(Edge(to,from,0,0,-cost)); int tot=edges.size(); G[from].push_back(tot-2); G[to].push_back(tot-1);}bool BF(int s,int t,int& flow,LL& cost){ //cout<<n<<" "<<m<<endl; //cout<<"flag"<<endl; for(int i=0; i<=2*n; i++) d[i]=INF; memset(inq,0,sizeof(inq)); d[s]=0,inq[s]=1,p[s]=0; queue<int> q; q.push(s),a[s]=INF; while(!q.empty()) { int u=q.front(); q.pop(),inq[u]=0; for(int i=0; i<G[u].size(); i++) { Edge& e=edges[G[u][i]]; if(e.cap>e.flow&&d[e.to]>d[u]+e.cost) { d[e.to]=d[u]+e.cost; p[e.to]=G[u][i]; a[e.to]=min(a[u],e.cap-e.flow); if(!inq[e.to]) { q.push(e.to); inq[e.to]=1; } } } } if(d[t]==INF) return false; flow+=a[t]; cost+=(LL)d[t]*(LL)a[t]; for(int u=t; u!=s; u=edges[p[u]].from) { edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; } return true;}int mincostmaxflow(int s,int t,LL& cost){ int flow=0; cost=0; while(BF(s,t,flow,cost)); return cost;}double dist(Node a,Node b){ return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));}void make(int x){ init(2*n); for(int i=1; i<=n-1; i++) { addedges(s,i,INF,1); addedges(i,t,worker[i].kind[x],0); addedges(s,i+n-1,worker[i].kind[x],0); //cout<<s<<" "<<i<<" "<<INF<<" "<<1<<endl; //cout<<i<<" "<<t<<" "<<worker[i].kind[x]<<" "<<0<<endl; //cout<<s<<" "<<i+n-1<<" "<<worker[i].kind[x]<<" "<<0<<endl; for(int j=1; j<=n-1; j++) { if(i==j) continue; double dis=dist(worker[i],worker[j]); if(worker[i].b+worker[i].p+dis<=worker[j].b) { //cout<<i+n-1<<" "<<j<<" "<<INF<<" "<<0<<endl; addedges(i+n-1,j,INF,0); } } } //cout<<"flag"<<endl;}void solve(){ int ans=0; for(int i=1; i<=m; i++) { make(i); //cout<<s<<" "<<t<<endl; ans+=mincostmaxflow(s,t,cost); } printf("%d\n",ans);}int main(){#ifdef debug freopen("in.in","r",stdin);#endif // debug int cas; scanf("%d",&cas); while(cas--) { scanf("%d%d",&n,&m); scanf("%d%d",&stx,&sty); s=0,t=2*(n-1)+1; //cout<<s<<" "<<t<<endl; for(int i=1; i<=n-1; i++) { scanf("%d%d%d%d",&worker[i].x,&worker[i].y,&worker[i].b,&worker[i].p); for(int j=1; j<=m; j++) scanf("%d",&worker[i].kind[j]); } solve(); } return 0;}
0 0
- Hdu 4494 Teamwork(最小费用流)
- HDU 4494 Teamwork 最小费用最大流
- HDU 4494 Teamwork(最大流或最小费用流)
- HDU ACM 4494 Teamwork 最小费用最大流
- hdu 4494 Teamwork(费用流)
- HDU 4494 Teamwork(最大流-Dinic+最小费用最大流-mcmf)
- hdu 4494 Teamwork(多源多汇最小费用最大流,巧妙构图)
- hdoj 4494 Teamwork 【最小费用最大流】
- HDU 4494 Teamwork 费用流/网络流
- hdu 4494 Teamwork (可行流的最小流)
- HDU 4494 Teamwork 2013通化邀请赛 B题 费用流
- 费用流+建图——Teamwork ( HDU 4494 )
- hdu 4494 Teamwork 网络流
- HDU 4494 - Teamwork(最大流)
- hdu 4067(最小费用最大流)
- hdu 4862 Jump(最小费用流)
- HDU 1533(最小费用流)
- hdu 1533 (最小费用流)
- java序列化和反序列化
- mybatis+spring MVC启动的时候出现Mapped Statements collection does not contain value for
- Git命令大全
- js中实现一次性替换掉字符串中多次出现的某一子字符串
- poj 2533 Longest Ordered Subsequence
- Hdu 4494 Teamwork(最小费用流)
- HDU 5802 Windows 10 (贪心)
- Linux 批量修改文件名
- 设计模式六大原则(3)依赖倒置原则(Dependence Inversion Principle)
- 尼姆博弈
- 设计模式学习之路 - 模板方法模式 - 专注于流程
- 1050. 螺旋矩阵(25)-PAT乙级真题
- 常用JS键盘鼠标事件
- HDU:3665 Seaside(dijkstra算法求最短路径)