题意
类似的一道排队等候,算最小总等待时间的题目。
思路
但是这道题的边数很多,直接跑会tle,可以动态加边,就是先连上倒数第一次操作的边,跑一遍费用流,然后对使用了倒数第一条边的点,连上相应的倒数第二条边。以此类推
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const ll mod = 2147483648; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 309; int n,m; int p[maxn],mp[maxn][maxn]; struct E { int v,val,cost; int nxt; }edge[8000009]; int head[maxn*maxn],gtot; void addedge(int u,int v,int val, int cost){ edge[gtot].v = v; edge[gtot].val = val; edge[gtot].cost = cost; edge[gtot].nxt = head[u]; head[u] = gtot++; edge[gtot].v = u; edge[gtot].val = 0; edge[gtot].cost = -cost; edge[gtot].nxt = head[v]; head[v] = gtot++; } int vis[maxn*maxn],pre[maxn*maxn],path[maxn*maxn]; ll dis[maxn*maxn]; bool spfa(int s,int t){ for(int i=s; i<=t; i++) dis[i] = inff,pre[i] = -1,vis[i] = 0; queue<int>que; que.push(s); vis[s] = 1; dis[s] = 0; while(!que.empty()){ int u = que.front(); que.pop(); vis[u] = 0; for(int i=head[u]; ~i; i =edge[i].nxt){ int v = edge[i].v,val = edge[i].val, cost = edge[i].cost; if(val > 0 && dis[v] > dis[u] + cost){ dis[v] = dis[u] + cost; pre[v] = u; path[v] = i; if(vis[v] == 0) { vis[v] = 1; que.push(v); } } } } return pre[t] != -1; } int sp = 0; ll mcmf(int s,int t){ ll flow = 0, cost = 0; while(spfa(s, t)){ int f = inf; for(int i=t; i!=s; i=pre[i]){ f = min(f, edge[path[i]].val); } flow += f; cost += 1ll*f * dis[t]; for(int i=t; i!=s; i=pre[i]){ edge[path[i]].val -= f; edge[path[i]^1].val += f; } int la = edge[path[t]^1].v + 1; int p = (la - 1 - n)/sp + 1; int b = (la - 1 - n)% sp + 1; addedge(la, t, 1, 0); for(int i=1; i<=n; i++){ addedge(i, la, 1, mp[i][p] * b); } } return cost; } int main(){ memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); rep(i, 1, n) scanf("%d", &p[i]),sp += p[i]; rep(i, 1, n) rep(j, 1, m) scanf("%d", &mp[i][j]); int s = 0, t = n+m*sp+1; for(int i=1; i<=n; i++) addedge(s, i, p[i], 0); for(int i=1; i<=m; i++){ addedge(n + (i-1)*sp + 1, t, 1, 0); } for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ addedge(i, n + (j-1)*sp + 1, 1, mp[i][j]); } } printf("%lld ", mcmf(s, t)); return 0; }