【分析】
之前打的那种KM会TLE。。。
why??明明说n^3的啊?
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 410
#define Maxm 160010
// #define INF 0x7fffffff
#define LL long long
const LL INF=1LL<<; LL mymin(LL x,LL y) {return x<y?x:y;}
LL mymax(LL x,LL y) {return x>y?x:y;} int N; int n,m;
int match[Maxn],op[Maxn];
LL eg[Maxn][Maxn],lx[Maxn],ly[Maxn],slack[Maxn];
int visx[Maxn],visy[Maxn],pre[Maxn]; void mh(int y)
{
for(int x,z;y>;y=z)
{
x=pre[y];z=op[x];
op[x]=y;match[y]=x;
}
} int nt;
void ffind(int st)
{
for(int i=;i<=N;i++) slack[i]=INF;
queue<int> q;nt++;
q.push(st);visx[st]=nt;
while ()
{
while(!q.empty())
{
int x=q.front();q.pop();
for(int y=;y<=N;y++)
if(visy[y]!=nt)
{
if(lx[x]+ly[y]==eg[x][y])
{
pre[y]=x;
if(!match[y]) {mh(y);return;}
q.push(match[y]);
visx[match[y]]=nt;visy[y]=nt;
}
else if(slack[y]>lx[x]+ly[y]-eg[x][y])
slack[y]=lx[x]+ly[y]-eg[x][y],pre[y]=x;
}
}
LL delta=INF;
for(int y=;y<=N;y++) if(visy[y]!=nt) delta=mymin(delta,slack[y]);
for(int i=;i<=N;i++)
{
if(visx[i]==nt) lx[i]-=delta;
if(visy[i]==nt) ly[i]+=delta;
else slack[i]-=delta;
}
for(int i=;i<=N;i++) if(visy[i]!=nt&&slack[i]==)
{
if(!match[i]) {mh(i);return;}
q.push(match[i]);
visx[match[i]]=nt;visy[i]=nt;
}
}
} LL KM()
{
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
for (int i=;i<=N;i++) lx[i]=ly[i]=,op[i]=match[i]=;
for (int i=;i<=N;i++)
for (int j=;j<=N;j++)
lx[i]=mymax(lx[i],eg[i][j]);
for (int i=;i<=N;i++) ffind(i);
} void output()
{
LL ans=;
for(int i=;i<=N;i++) ans+=lx[i]+ly[i];
printf("%lld\n",ans);
for(int i=;i<=n;i++) if(eg[i][op[i]]!=) printf("%d ",op[i]);
else printf("0 ");printf("\n");
} void init()
{
int l;
memset(eg,,sizeof(eg));
scanf("%d%d%d",&n,&m,&l);
for(int i=;i<=l;i++)
{
int x,y;
LL c;
scanf("%d%d%lld",&x,&y,&c);
eg[x][y]=mymax(eg[x][y],c);
}
N=mymax(n,m);
} int main()
{
init();
KM();
output();
return ;
}