标签:deque amp efi mem struct eof 取绝对值 ace inf
焦作上的一道,网络流24题中的原题....
离散化那里不太熟
spfa的slf优化能快200ms
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=10010;
const int maxm=100010;
const int inf=0x3f3f3f3f;
struct edge{
int to,next,cap,flow,cost;
}e[maxm];
int head[maxn],tol,cost;
int pre[maxn],dis[maxn];
bool vis[maxn];
int N;
void init(int n){
N=n;tol=1;cost=0;
memset(head,0,sizeof(head));
}
void addedge(int u,int v,int cap,int cost){
++tol;
e[tol].to=v;e[tol].next=head[u];e[tol].cap=cap;e[tol].cost=cost;e[tol].flow=0;
head[u]=tol;
++tol;
e[tol].to=u;e[tol].next=head[v];e[tol].cap=0;e[tol].flow=0;e[tol].cost=-cost;
head[v]=tol;
}
bool spfa(int s,int t){
deque<int>q;
for (int i = 0; i <=N ; ++i) {
dis[i]=inf;
vis[i]=false;
pre[i]=-1;
}
dis[s]=0;
vis[s]=true;
q.push_front(s);
while (!q.empty()){
int u=q.front();
q.pop_front();
vis[u]=0;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(e[i].cap>e[i].flow&&dis[v]>dis[u]+e[i].cost){
dis[v]=dis[u]+e[i].cost;
pre[v]=i;
if(!vis[v]){
vis[v]=true;
if(!q.empty()){
if(dis[v]<dis[q.front()]) q.push_front(v);
else q.push_back(v);
}
else q.push_back(v);
}
}
}
}
if(pre[t]==-1) return false;
else return true;
}
int mcfc(int s,int t){
int flow=0;
while (spfa(s,t)){
int minn=inf;
for(int i=pre[t];i!=-1;i=pre[e[i^1].to]){
if(minn>e[i].cap-e[i].flow){
minn=e[i].cap-e[i].flow;
}
}
for(int i=pre[t];i!=-1;i=pre[e[i^1].to]){
e[i].flow+=minn;
e[i^1].flow-=minn;
cost+=e[i].cost*minn;
}
flow+=minn;
}
return flow;
}
struct node{
int a,b,c;
}f[300];
map<int,int>mp;
int main(){
int T;scanf("%d",&T);
int n,m,k,cnt;int a,b,w;
while (T--){
scanf("%d%d%d",&n,&m,&k);
cnt=0;mp.clear();
for (int i = 1; i <= k; ++i) {
scanf("%d%d%d",&a,&b,&w);
f[i]=node{a,b+1,w};
mp[a]=mp[b+1]=1;
}
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
int id=it->first;
mp[id]=++cnt;
}
init(cnt+5);
int s=0,t=cnt+1;
addedge(0,1,m,0);
addedge(cnt,t,m,0);
for (int i = 1; i <cnt ; ++i) {
addedge(i,i+1,inf,0);
}
for (int j = 1; j <=k ; ++j) {
addedge(mp[f[j].a],mp[f[j].b],1,-f[j].c);
}
mcfc(s,t);
printf("%d\n",-cost);
}
}
标签:deque amp efi mem struct eof 取绝对值 ace inf
原文地址:https://www.cnblogs.com/smallocean/p/9685916.html