这个地方最开心的就是原来输出每条路径的流还是挺简单的。。
只要把G, edges提出来,就能在外面输出了。。。
但是这里还是TLE了,因为紫书给的最小费用流的算法不是很高效。。
先不深究了。。。
分析:(这题算是 最小权匹配。 可用最小费用流求解。。 相当于二分图最大匹配可以用最大流求解一样。
1:要知道为什么这里会变成最小费用最大流。。费用是什么,,流是什么。。。
2:还有就是要注意题目说的是所有人避难时间的总和,所以会有单位价值加起来。。
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a));
#define sf scanf
#define pf printf
#define LL long long
#define bug1 cout<<"bug1"<<endl;
#define bug2 cout<<"bug2"<<endl;
#define bug3 cout<<"bug3"<<endl;
const int maxn = 200 + 10;
const int INF = 1000000000;
struct Edge {
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w):from(u),to(v),cap(c),flow(f),cost(w) {}
};
vector<int> G[maxn];
vector<Edge> edges;
struct MCMF {
int n, m;
int inq[maxn]; // 是否在队列中
int d[maxn]; // Bellman-Ford
int p[maxn]; // 上一条弧
int a[maxn]; // 可改进量
void init(int n) {
this->n = n;
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
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));
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BellmanFord(int s, int t, int flow_limit, int& flow, int& cost) {
for(int i = 0; i < n; i++) d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
queue<int> Q;
Q.push(s);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
}
}
}
if(d[t] == INF) return false;
if(flow + a[t] > flow_limit) a[t] = flow_limit - flow;
flow += a[t];
cost += d[t] * a[t];//这个是单位费用,如果要求走过就算的话,那么一般设置流量为1,
for(int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
}
return true;
}
// 需要保证初始网络中没有负权圈
int MincostFlow(int s, int t, int flow_limit, int& cost) {
int flow = 0; cost = 0;
while(flow < flow_limit && BellmanFord(s, t, flow_limit, flow, cost));
return flow;//返回的流量,但其实是没有用的,求的最小费用在&cost中求了
}
};
MCMF g;
int x[maxn],y[maxn];
int xx[maxn],yy[maxn];
int contain[maxn];
int dis(int x,int y,int xx,int yy){
return abs(xx-x)+abs(yy-y)+1;
}
int main(){
int n,m;
while(~sf("%d%d",&n,&m)){
g.init(n+m+2);int s=n+m;int t=n+m+1;
for(int i=0;i<n;++i){
sf("%d%d%d",&x[i],&y[i],&contain[i]);
}
for(int i=n;i<=n+m-1;++i){
sf("%d%d%d",&xx[i],&yy[i],&contain[i]);
}
int mincost=0;
for(int i=0;i<n;++i){
for(int j=n;j<=n+m-1;++j){
int a;
sf("%d",&a);
if(a)mincost+=a*dis(x[i],y[i],xx[j],yy[j]);
}
}
for(int i=0;i<n;++i){
for(int j=n;j<=n+m-1;++j){
g.addedge(i,j,INF,dis(x[i],y[i],xx[j],yy[j]));
}
}
for(int i=0;i<n;++i){
g.addedge(s,i,contain[i],0);
}
for(int i=n;i<=n+m-1;++i){
g.addedge(i,t,contain[i],0);
}
int cost;
g.MincostFlow(s,t,INF,cost);
if(cost<mincost){
pf("SUBOPTIMAL\n");
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
pf("%d%c",edges[G[i][j]].flow,j==m-1?'\n':' ');
}
}
}
else pf("OPTIMAL\n");
}
}