description
神犇航空有K架飞机,为了简化问题,我们认为每架飞机都是相同的。神犇航空的世界中有N个机场,以0…N-1编号,其中0号为基地机场,每天0时刻起飞机才可以从该机场起飞,并不晚于T时刻回到该机场。一天,神犇航空接到了M个包机请求,每个请求为在s时刻从a机场起飞,在恰好t时刻到达b机场,可以净获利c。设计一种方案,使得总收益最大。
Input
第一行,4个正整数N,M,K,T,如题目描述中所述;
以下N行,每行N个整数,描述一个N*N的矩阵t,ti,j表示从机场i空载飞至机场j,需要时间ti,j;
以下N行, 每行N个整数,描述一个N*N的矩阵f,fi,j表示从机场i空载飞至机场j,需要费用fi,j;
以下M行,每行5个整数描述一个请求,依次为a,b,s,t,c。
Output
仅一行,一个整数,表示最大收益。
Sample Input
2 1 1 10
0 5
5 0
0 5
5 0
0 1 0 5 10
Sample Output
5
Hint
数据规模及约定
对于10%的测试数据,K=1;
另有20%的测试数据,K=2;
对于全部的测试数据,N,M<=200,K<=10,T<=3000,ti,j<=200,fi,j<=2000,0<=a,b<N,0<=s<=t<=T,0<=c<=10000,ti,i=fi,i=0,ti,j<=ti,k+tk,j,fi,j<=fi,k+fk,j。
solution
将请求拆成两个点,之间的费用就是净收益ccc,流量为111,一个请求只完成一次
然后再根据时间限制判断能否将请求的起终点与源点汇点连边,流量infinfinf
从000飞到请求起点的时间≤\le≤请求要求的起飞时间
从请求要求的结束时间➕从请求终点飞回000点时间≤T\le T≤T
但不一定是完成一个请求后立马飞回000点,或许直接飞往下一个请求的起点更优
所以还需要对请求之间进行两两建边
请求完成时刻➕飞到下一个请求的起点所有时间≤\le≤下一个请求的起飞时刻
最大化收益那就跑最大费用最大流
code
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 205
#define maxm 100005
#define inf 0x3f3f3f3f
struct node {int a, b, s, t, c;
}Q[maxn];
struct Edge {int nxt, to, w, flow;
}edge[maxm << 1];
queue < int > q;
int n, m, K, T, st, ed, cnt, Time, cost;
int ti[maxn][maxn], w[maxn][maxn];
int dis[maxm], head[maxm], vis[maxm], pre[maxm];void addedge( int u, int v, int w, int flow ) {edge[cnt].nxt = head[u];edge[cnt].to = v;edge[cnt].w = w;edge[cnt].flow = flow;head[u] = cnt ++;
}bool spfa() {for( int i = 0;i <= ed;i ++ ) dis[i] = -inf, vis[i] = 0, pre[i] = -1;//memset(dis,-0x3f,sizeof(dis))无法初始化极小值 q.push( st );dis[st] = 0, vis[st] = 1;while( ! q.empty() ) {int u = q.front(); q.pop();vis[u] = 0;for( int i = head[u];~ i;i = edge[i].nxt ) {int v = edge[i].to;if( dis[v] < dis[u] + edge[i].w && edge[i].flow ) {dis[v] = dis[u] + edge[i].w;pre[v] = i;if( ! vis[v] ) {q.push( v );vis[v] = 1;}}}}return dis[ed] != -inf;
}void MCMF() {while( spfa() ) {int flow = inf;for( int i = pre[ed];~ i;i = pre[edge[i ^ 1].to] )flow = min( flow, edge[i].flow );for( int i = pre[ed];~ i;i = pre[edge[i ^ 1].to] ) {edge[i].flow -= flow;edge[i ^ 1].flow += flow;cost += edge[i].w * flow;}}
}int main() {memset( head, -1, sizeof( head ) );scanf( "%d %d %d %d", &n, &m, &K, &T );for( int i = 0;i < n;i ++ )for( int j = 0;j < n;j ++ )scanf( "%d", &ti[i][j] );for( int i = 0;i < n;i ++ )for( int j = 0;j < n;j ++ )scanf( "%d", &w[i][j] );for( int i = 1;i <= m;i ++ )scanf( "%d %d %d %d %d", &Q[i].a, &Q[i].b, &Q[i].s, &Q[i].t, &Q[i].c );st = 0, ed = ( m << 1 ) + 2;for( int i = 1;i <= m;i ++ ) {addedge( i << 1, i << 1 | 1, Q[i].c, 1 );addedge( i << 1 | 1, i << 1, -Q[i].c, 0 );if( Q[i].t + ti[Q[i].b][0] <= T ) {addedge( i << 1 | 1, ed, -w[Q[i].b][0], inf );addedge( ed, i << 1 | 1, w[Q[i].b][0], 0 );}else continue;if( ti[0][Q[i].a] <= Q[i].s ) {addedge( st + 1, i << 1, -w[0][Q[i].a], inf );addedge( i << 1, st + 1, w[0][Q[i].a], 0 );}for( int j = 1;j <= m;j ++ )if( Q[i].t + ti[Q[i].b][Q[j].a] <= Q[j].s ) {addedge( i << 1 | 1, j << 1, -w[Q[i].b][Q[j].a], inf );addedge( j << 1, i << 1 | 1, w[Q[i].b][Q[j].a], 0 );}}addedge( st, st + 1, 0, K );addedge( st + 1, st, 0, 0 );MCMF();printf( "%d", cost );return 0;
}
Dinic费用流版本
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 205
#define maxm 100005
#define inf 0x3f3f3f3f
struct node {int a, b, s, t, c;
}Q[maxn];
struct Edge {int nxt, to, w, flow;
}edge[maxm << 1];
queue < int > q;
int n, m, K, T, st, ed, cnt = 1, Time, cost;
int ti[maxn][maxn], w[maxn][maxn];
int dis[maxm], head[maxm], vis[maxm];void addedge( int u, int v, int w, int flow ) {cnt ++;edge[cnt].nxt = head[u];edge[cnt].to = v;edge[cnt].w = w;edge[cnt].flow = flow;head[u] = cnt;
}bool spfa() {for( int i = 0;i <= ed;i ++ ) dis[i] = -inf, vis[i] = 0;//memset(dis,-0x3f,sizeof(dis))无法初始化极小值 q.push( st );dis[st] = 0, vis[st] = 1;while( ! q.empty() ) {int u = q.front(); q.pop();vis[u] = 0;for( int i = head[u];i;i = edge[i].nxt ) {int v = edge[i].to;if( dis[v] < dis[u] + edge[i].w && edge[i].flow ) {dis[v] = dis[u] + edge[i].w;if( ! vis[v] ) {q.push( v );vis[v] = 1;}}}}return dis[ed] != -inf;
}int dfs( int u, int cap ) {if( u == ed ) return cap;vis[u] = Time;int flow = 0;for( int i = head[u];i;i = edge[i].nxt ) {int v = edge[i].to;if( ( vis[v] != Time || v == ed ) && edge[i].flow && dis[v] == dis[u] + edge[i].w ) {int tmp = dfs( v, min( cap, edge[i].flow ) );if( ! tmp ) continue;edge[i].flow -= tmp;edge[i ^ 1].flow += tmp;flow += tmp;cap -= tmp;cost += tmp * edge[i].w;if( ! cap ) break;}}return flow;
}void Dinic() {while( spfa() ) {do {Time ++;dfs( st, inf );} while( vis[ed] == Time );}
}int main() {scanf( "%d %d %d %d", &n, &m, &K, &T );for( int i = 0;i < n;i ++ )for( int j = 0;j < n;j ++ )scanf( "%d", &ti[i][j] );for( int i = 0;i < n;i ++ )for( int j = 0;j < n;j ++ )scanf( "%d", &w[i][j] );for( int i = 1;i <= m;i ++ )scanf( "%d %d %d %d %d", &Q[i].a, &Q[i].b, &Q[i].s, &Q[i].t, &Q[i].c );st = 0, ed = ( m << 1 ) + 2;for( int i = 1;i <= m;i ++ ) {addedge( i << 1, i << 1 | 1, Q[i].c, 1 );addedge( i << 1 | 1, i << 1, -Q[i].c, 0 );if( Q[i].t + ti[Q[i].b][0] <= T ) {addedge( i << 1 | 1, ed, -w[Q[i].b][0], inf );addedge( ed, i << 1 | 1, w[Q[i].b][0], 0 );}else continue;if( ti[0][Q[i].a] <= Q[i].s ) {addedge( st + 1, i << 1, -w[0][Q[i].a], inf );addedge( i << 1, st + 1, w[0][Q[i].a], 0 );}for( int j = 1;j <= m;j ++ )if( Q[i].t + ti[Q[i].b][Q[j].a] <= Q[j].s ) {addedge( i << 1 | 1, j << 1, -w[Q[i].b][Q[j].a], inf );addedge( j << 1, i << 1 | 1, w[Q[i].b][Q[j].a], 0 );}}addedge( st, st + 1, 0, K );addedge( st + 1, st, 0, 0 );Dinic();printf( "%d", cost );return 0;
}