#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int M=199999;
int n,m,s,t;int dis[M];;
int nex[M],head[M],cos[M],to[M],tot,pre[M],cap[M],vis[M],flo[M],id[M];
struct st2{
int f,b;
}a[M];
int add(int x,int y,int z,int w){
cos[tot]=z;
cap[tot]=w;
nex[++tot]=head[x];
to[tot]=y;
head[x]=tot;
}
int spfa(int s,int t){
queue <int> q;
memset(dis,127,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
int INF=dis[0];
dis[s]=0;
vis[s]=1;
q.push(s);
flo[s]=INF;
while(!q.empty()){
int x=q.front();
vis[x]=0;
q.pop();
for(int i=head[x];i;i=nex[i])
{
int tmp=to[i];
if(dis[tmp]>dis[x]+cos[i-1]&&cap[i-1]){
dis[tmp]=dis[x]+cos[i-1];
pre[tmp]=x;
flo[tmp]=min(flo[x],cap[i-1]);
id[tmp]=i-1;
if(!vis[tmp]){
q.push(tmp);
vis[tmp]=1;
}
}
}
}
if(dis[t]>=INF)return 0;
return 1;
}
struct st{
int cost=0;
int flow=0;
};
st maxflow(int s,int t){
st a;
while(spfa(s,t)){
int k=t;
while(k!=s){
cap[id[k]]-=flo[t];
cap[id[k]^1]+=flo[t];
k=pre[k];
}
a.flow++;
a.cost+=dis[t];
}
return a;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
add(i,n+i,0,1);
add(n+i,i,0,0);
}
int cnt=1;
for(int i=1;i<=m;i++){
int x,y,z,w;
scanf("%d%d%d",&x,&y,&z);
add(x+n,y,z,1);
add(y,x+n,-z,0);
}
//printf("%d %d",a[1].b,a[n].f);
st d=maxflow(1+n,n);
printf("%d %d",d.flow,d.cost);
return 0;
}
晨跑
最新推荐文章于 2018-09-25 09:08:19 发布