题解:最小费用流+二分图模型;

左边表示出这个点,右边表示入这个点;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=10009;
const int inf=1000000000; int n,m;
int a[maxn];
struct Edge{
int from,to,cap,flow,cost;
};
vector<int>G[maxn];
vector<Edge>edges;
int addedge(int x,int y,int z,int w){
Edge e;
e.from=x;e.to=y;e.cap=z;e.flow=0;e.cost=w;
edges.push_back(e);
e.from=y;e.to=x;e.cap=0;e.flow=0;e.cost=-w;
edges.push_back(e);
int c=edges.size();
G[x].push_back(c-2);
G[y].push_back(c-1);
} int s,t,totn;
queue<int>q;
int inq[maxn];
int d[maxn];
int p[maxn];
int Spfa(int &nowflow,int &nowcost){
for(int i=1;i<=totn;++i){
d[i]=inf;inq[i]=0;
}
q.push(s);inq[s]=1;d[s]=0;p[s]=0;
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
for(int i=0;i<G[x].size();++i){
Edge e=edges[G[x][i]];
if(e.cap>e.flow&&d[x]+e.cost<d[e.to]){
d[e.to]=d[x]+e.cost;
p[e.to]=G[x][i];
if(!inq[e.to]){
q.push(e.to);
inq[e.to]=1;
}
}
}
} if(d[t]==inf)return 0;
int f=inf,x=t;
while(x!=s){
Edge e=edges[p[x]];
f=min(f,e.cap-e.flow);
x=e.from;
}
nowflow+=f;nowcost+=f*d[t];
x=t;
while(x!=s){
edges[p[x]].flow+=f;
edges[p[x]^1].flow-=f;
x=edges[p[x]].from;
}
return 1;
} int Mcmf(){
int flow=0,cost=0;
while(Spfa(flow,cost));
return cost;
} int minit(){
edges.clear();
for(int i=1;i<=totn;++i)G[i].clear();
} int main(){
scanf("%d%d",&n,&m);
s=n+n+1;t=totn=s+1;minit(); for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
addedge(s,i+n,1,a[i]);
}
for(int i=1;i<=m;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(x>y)swap(x,y);
addedge(x,n+y,1,z);
}
for(int i=1;i<=n;++i){
addedge(s,i,1,0);
addedge(i+n,t,1,0);
}
printf("%d\n",Mcmf());
return 0;
}

  

最新文章

  1. Linux 使用iftop命令查看服务器流量
  2. 三:shell运算符
  3. atitit.常用编程语言的性能比较 c c++ java
  4. C#快速排序详解
  5. C# - 自动属性
  6. GCC参数详解
  7. bzoj 3669: [Noi2014]魔法森林 动态树
  8. Java基础知识强化之IO流笔记19:FileOutputStream的三个write方法
  9. 转:Thumbs.db是什么文件?是病毒吗?怎么处理?
  10. NSStirng、NSArray、以及枚举(Method小集合)
  11. BZOJ 1052 HAOI2007 覆盖问题 二分法答案+DFS
  12. 判断点在不在多边形范围内c#
  13. shapefile添加字段 设置文件名为字段内容
  14. EF 利用PagedList进行分页并结合查询 方法2
  15. ES6定型数组
  16. Future 模式简介
  17. 用MyEclipse开发REST Web Service
  18. SqlServer语句
  19. C#语言正则用法
  20. Django admin 产生&#39;WSGIRequest&#39; object has no attribute &#39;user&#39;的错误

热门文章

  1. 「SP2713」GSS4 - Can you answer these queries IV
  2. 「React Native笔记」在React的 setState 中操作数组和对象的多种方法(合集)
  3. 前端学习笔记系列一:15vscode汉化、快速复制行、网页背景图有效设置、 dl~dt~dd标签使用
  4. MySQL 加密 压缩
  5. SQLSERVER|CDC 日志变更捕获机制
  6. Ajax发送PUT/DELETE请求时出现错误的原因及解决方案
  7. 数据可视化-gojs插件使用技巧总结
  8. imp.load_source的用法
  9. 015.CI4框架CodeIgniter数据库操作之:Query带参数查询数
  10. jquery动态选中radio,获取radio选中值