记录编号 |
60196 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[NOI 2006]最大获利 |
最终得分 |
100 |
用户昵称 |
cstdio |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
1.986 s |
提交时间 |
2013-05-19 11:50:57 |
内存使用 |
1.36 MiB |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
#define min(a,b)(a<b?a:b)
const int SIZEN=5001,SIZEG=55003,INF=0x7fffffff;
int N,M;
int sumc=0;
class EDGE{
public:
int from,to,cap,flow;
};
vector<EDGE> edges;
vector<int> c[SIZEG];//邻接表
//节点1~N是基站,N+1~N+M是客户,N+M+1是源,N+M+2是汇
int s,t,n;//orz= =
int d[SIZEG]={0},cur[SIZEG]={0};
bool BFS(void){
bool vis[SIZEG]={0};
//memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=0,vis[s]=true;
int x,i;
//#define e edges[c[x][i]]
while(!Q.empty()){
x=Q.front();Q.pop();
for(i=0;i<c[x].size();i++){
EDGE& e=edges[c[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=true;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a){//节点x
if(x==t||a==0) return a;
int flow=0,f;
int i;
//#define e edges[c[x][i]]
for(i=cur[x];i<c[x].size();i++){
EDGE& e=edges[c[x][i]];
if(d[x]+1==d[e.to]){
f=DFS(e.to,min(a,e.cap-e.flow));
if(f>0){
e.flow+=f;
edges[c[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(!a) break;
}
}
}
return flow;
}
int maxflow(void){//dinic
int flow=0;
while(BFS()){
//cout<<"动次打次"<<endl;
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
void addedge(int from,int to,int cap){
edges.push_back((EDGE){from,to,cap,0});
edges.push_back((EDGE){to,from,0,0});
int k=edges.size();
c[from].push_back(k-2);
c[to].push_back(k-1);
}
void init(void){//建图部分,可根据需要修改
scanf("%d%d",&N,&M);
int p[SIZEN]={0};
int i,a,b,w;
for(i=1;i<=N;i++) scanf("%d",&p[i]);
for(i=1;i<=M;i++){
scanf("%d%d%d",&a,&b,&w);
sumc+=w;
addedge(N+i,a,INF),addedge(N+i,b,INF);
addedge(N+M+1,N+i,w);
}
for(i=1;i<=N;i++) addedge(i,N+M+2,p[i]);
s=N+M+1,t=N+M+2;
}
int main(){
freopen("profit.in","r",stdin);
freopen("profit.out","w",stdout);
//int tp=clock();
//cout<<tp<<endl;
init();
cout<<sumc-maxflow()<<endl;
//cout<<"time:"<<clock()-tp<<endl;
return 0;
}