BZOJ传送门
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 332 Solved: 164
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
3 3
2 3 4
1 1 2 2
1 2 3 5
1 3 3 2
Sample Output
14
HINT
Source
单纯形
不会单纯形,跑网络流
在BZOJ居然Rank1,截图纪念一下QuQ
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define N 1050
#define U 100000
#define INF 2139062143
inline int in(int x=0,int v=1,char ch=getchar()){
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();if(ch=='-') v=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*v;
}
struct NetWork{
struct Edge{int fr,to,cap,flow,cost;}edge[N*900];vector<int> g[N];
int n,s,t,m,b[N],d[N],a[N],p[N],cost,cnt,v[N][2];
inline void Add_Edge(int fr,int to,int flow,int cost){
edge[cnt++]=((Edge){fr,to,flow,0,cost});
edge[cnt++]=((Edge){to,fr,0,0,-cost});
g[fr].push_back(cnt-2);g[to].push_back(cnt-1);
}
inline int spfa(){
queue<int> q;bool b[N];int x,l;
memset(b,0,sizeof(b));memset(d,0x7f,sizeof(d));
b[s]=1,d[s]=0,a[s]=INF;q.push(s);
while(!q.empty()){
x=q.front();q.pop();b[x]=false;l=g[x].size();
for(int i=0;i<l;i++){
Edge& e=edge[g[x][i]];
if(e.cap>e.flow&&d[e.to]>d[x]+e.cost){
d[e.to]=d[x]+e.cost;
p[e.to]=g[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
if(!b[e.to]){b[e.to]=1;q.push(e.to);}
}
}
}
if(d[t]==INF) return 0;
cost+=d[t]*a[t];
for(x=t;x!=s;x=edge[p[x]].fr) edge[p[x]].flow+=a[t],edge[p[x]^1].flow-=a[t];
return 1;
}
void init(){
n=in(),m=in();int x,y,z;s=0,t=n+1;
Add_Edge(s,1,U,0);
for(int i=1;i<=n;i++) x=in(),Add_Edge(i,i+1,U-x,0);
for(int i=1;i<=m;i++){
x=in();
for(int j=1;j<=x;j++) v[j][0]=in(),v[j][1]=in();
y=in();
for(int j=1;j<=x;j++) Add_Edge(v[j][0],v[j][1]+1,INF,y);
}
while(spfa());
printf("%d\n",cost);
}
}s;
int main(){s.init();return 0;}