#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+5;
int n,m,S,T,head[N],pre[N],d[N],cnt=1; ll ans=1e9,ans1,ans2;
struct nd{ int ne,to,w,f; }e[N*20];
void in(int x,int y,int w,int f){ ++cnt;e[cnt].to=y;e[cnt].ne=head[x];head[x]=cnt;e[cnt].w=w;e[cnt].f=f; ++cnt;e[cnt].to=x;e[cnt].ne=head[y];head[y]=cnt;e[cnt].w=0;e[cnt].f=-f; }
bool spfa(){ for(int i=1;i<=T;++i)d[i]=2e9; static bool inq[N]; static queue<int>q; for(int i=1;i<=T;++i)inq[i]=0; d[S]=0; q.push(S);inq[S]=1; while(!q.empty()){ int x=q.front();q.pop();inq[x]=0; for(int i=head[x];i;i=e[i].ne){ int y=e[i].to; if(d[y]>d[x]+e[i].f&&e[i].w>0){ d[y]=d[x]+e[i].f; pre[y]=i; if(!inq[y])inq[y]=1,q.push(y); } } } return d[T]!=2e9; }
void dinic(){ int flow=2e9; for(int i=pre[T];i;i=pre[e[i^1].to])flow=min(flow,e[i].w); for(int i=pre[T];i;i=pre[e[i^1].to])e[i].w-=flow,e[i^1].w+=flow; ans1+=flow; ans2+=flow*d[T]; }
int main(){ scanf("%d%d",&n,&m); for(int i=1,x,y,w;i<=m;++i){ scanf("%d%d%d",&x,&y,&w); in(x,y+n,1,w); } S=2*n+2; T=2*n+3; for(int i=1;i<=n;++i){ in(S,i,1,-1e9); in(S,i,1e9,0); in(i+n,T,1,-1e9); in(i+n,T,1e9,0); } S--; in(S,S+1,n-1,0); for(int i=1;i<=n+1;++i){ in(S,S+1,1,0); while(spfa())dinic(); ans=min(ans,ans2+(ll)2e9*n); } if(ans>=1e9)printf("NIE\n"); else printf("%d\n",ans); }
|