# 百度之星初赛B 度度熊的交易计划（最小费用可行流）

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int maxn = 505 + 5;
const int maxm = 40000 + 5;
int n, m;
int head[maxn], to[maxm], front[maxm], flow[maxm], cost[maxm], ppp;
int dis[maxn], minflow[maxn];
bool flag[maxn];
pair<int, int> par[maxn];
int a[maxn], b[maxn], c[maxn], d[maxn];
int G[maxn][maxn];

struct MIN_COST_MAX_FLOW{

void init() {
ppp = 0;
}

bool spfa(int s, int t)
{
int u, v;
for(int i = 0; i <= t; i++)
dis[i] = INF;
memset(flag, 0, sizeof(flag));
dis[s] = 0;
minflow[s] = INF;
queue <int> q;
q.push(s);
while(!q.empty())
{
u = q.front();
q.pop();
flag[u] = 0;
for(int i = head[u]; ~i; i = front[i])
{
v = to[i];
if(flow[i] && dis[v] > dis[u] + cost[i])
{
dis[v] = dis[u] + cost[i];
par[v] = (make_pair(u, i));
minflow[v] = min(minflow[u], flow[i]);
if(!flag[v])
{
flag[v] = 1;
q.push(v);
}
}
}
}
return dis[t] != INF;
}

int slove(int s, int t)
{
int ans = 0, p;
while(spfa(s, t))
{
if(dis[t] > 0)
break;
p = t;
while(p != s)
{
flow[par[p].second] -= minflow[t];
flow[par[p].second^1] += minflow[t];
p = par[p].first;
}
ans += dis[t] * minflow[t];
}
return -ans;
}

void add_edge(int u, int v, int f, int c)
{
to[ppp] = v, front[ppp] = head[u], flow[ppp] = f, cost[ppp] = c, head[u] = ppp++;
to[ppp] = u, front[ppp] = head[v], flow[ppp] = 0, cost[ppp] = -c, head[v] = ppp++;
}
}mcmf;

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while(cin >> n >> m) {
ppp = 0;
memset(G, 0x3f, sizeof(G));
int s = 0, t = n + 1;
for(int i = 1; i <= n; i++) {
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
}
while(m--) {
int u, v, val;
scanf("%d%d%d", &u, &v, &val);
if(u == v)
continue;
G[u][v] = G[v][u] = min(val, G[u][v]);
}
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
if(G[i][j] != INF) {
}
}
}
int ans = mcmf.slove(s, t);
printf("%d\n", ans);
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：百度之星初赛B 度度熊的交易计划（最小费用可行流） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)