记录编号 60140 评测结果 AAAAAAAAAA
题目名称 [NOI 2008]志愿者招募 最终得分 100
用户昵称 Gravatarcstdio 是否通过 通过
代码语言 C++ 运行时间 3.900 s
提交时间 2013-05-18 09:55:42 内存使用 0.88 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<map>
#include<set>
#include<deque>
#include<cstring>
#include<cmath>
using namespace std;
const int SIZEN=1010,INF=0x7fffffff;
int N,M;
class EDGE{
public:
	int from,to,cap,flow,cost;
	//两顶点,容量,流量,费用
};
deque<EDGE> edges;
deque<int> c[SIZEN];//邻接矩阵
int d[SIZEN]={0};//距离标号
int father[SIZEN]={0};//每个节点最短路上的前驱
int ansc=0;
void addedge(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();
	c[from].push_back(tot-2);
	c[to].push_back(tot-1);
}
void read(void){
	scanf("%d%d",&N,&M);
	int i,j;
	int s,t,c;
	int a[SIZEN]={0};
	for(i=1;i<=N;i++) scanf("%d",&a[i]);
	for(i=1;i<=M;i++){
		scanf("%d%d%d",&s,&t,&c);
		addedge(s,t+1,INF,c);
	}
	for(i=1;i<=N+1;i++){
		int temp=a[i]-a[i-1];
		if(temp>=0) addedge(0,i,temp,0);
		else addedge(i,N+2,-temp,0);
		if(i>1) addedge(i,i-1,INF,0);
	}
}
#define now edges[c[x][i]]
bool SPFA(int s,int t){
	deque<int> Q;
	bool inq[SIZEN]={0};
	int cf[SIZEN]={0};//到每个节点的最大可改进量"喜爱福"
	int i;
	for(i=0;i<=N+2;i++) d[i]=cf[i]=INF,father[i]=-1;
	d[s]=0,inq[s]=true,Q.push_back(s);
	int x;
	while(!Q.empty()){
		x=Q.front();inq[x]=false;Q.pop_front();
		for(i=0;i<c[x].size();i++){
			if(now.cap>now.flow&&d[now.to]>d[x]+now.cost){//可增广
				d[now.to]=d[x]+now.cost;
				father[now.to]=c[x][i];
				cf[now.to]=min(cf[x],now.cap-now.flow);
				if(!inq[now.to]){
					Q.push_back(now.to);
					inq[now.to]=true;
				}
			}
		}
	}
	if(d[t]==INF) return false;
	ansc+=cf[t]*d[t];
	x=t;
	while(x!=s){
		edges[father[x]].flow+=cf[t];
		edges[father[x]^1].flow-=cf[t];
		x=edges[father[x]].from;
	}
	return true;
}
void work(void){
	while(SPFA(0,N+2));
	printf("%d\n",ansc);
}
int main(){
	freopen("employee.in","r",stdin);
	freopen("employee.out","w",stdout);
	read();
	work();
	return 0;
}