度度熊的交易计划

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1192    Accepted Submission(s): 440

Problem Description

Input

1<=n<=500,
1<=m<=1000,
1<=a[i],b[i],c[i],d[i],k[i]<=1000,
1<=u[i],v[i]<=n

Output

Sample Input
2 1 5 5 6 1 3 5 7 7 1 2 1

Sample Output
23

1.建立超级源点S,对每个片区i连容量为b[i],费用为a[i]的单向边.
2.每个片区对可达的片区连容量为正无穷,费用为距离k[i]的双向边.
3.每个片区对汇点T连容量为d[i]，费用为c[i]的双向边.

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

const int INF = 999999999;
const int N = 600;
const int M = 200005;
struct Edge{
int u,v,cap,cost,next;
}edge[M];
int total;
bool vis[N];
void addEdge(int u,int v,int cap,int cost,int &k){
}
void init(){
tot = 0;
}
bool spfa(int s,int t,int n){
memset(vis,false,sizeof(vis));
for(int i=0;i<=n;i++){
low[i] = (i==s)?0:INF;
pre[i] = -1;
}
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
int v = edge[k].v;
if(edge[k].cap>0&&low[v]>low[u]+edge[k].cost){
low[v] = low[u] + edge[k].cost;
pre[v] = k; ///v为终点对应的边
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-1) return false;
return true;
}
int MCMF(int s,int t,int n){
int mincost = 0,minflow,flow=0;
while(spfa(s,t,n))
{
if(low[t]>=0) break;
minflow=INF+1;
for(int i=pre[t];i!=-1;i=pre[edge[i].u])
minflow=min(minflow,edge[i].cap);
flow+=minflow;
for(int i=pre[t];i!=-1;i=pre[edge[i].u])
{
edge[i].cap-=minflow;
edge[i^1].cap+=minflow;
}
mincost+=low[t]*minflow;
}
total=flow;
return mincost;
}

int main()
{
int n,m,h,tcase;
while(scanf("%d%d",&n,&m)!=EOF){

init();
int s = 0,t = n+1;
for(int i=1;i<=n;i++){
//if(a[i]>=mx) b[i] = 0;
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
}
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
}