手贱tot初始化的时候明显想着0却写成1,调试半天。。。。。

这样看来,在网络流方面明显 tot = 0 memset(head,-1,sizeof(head)) 更加方便一点

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1e3+10, maxm = 1e5+10,inf = 0x7f7f7f7f;

int n,m,s,t;
int tot,head[maxn];
struct Edge{
	int to,nxt,f,w;
}e[maxm<<1]; 
void add(int from,int to,int f,int w)
{
	e[tot].to = to; e[tot].nxt = head[from]; e[tot].f = f; e[tot].w = w; head[from] = tot++;
	e[tot].to = from; e[tot].nxt = head[to]; e[tot].f = 0; e[tot].w = -w; head[to] = tot++;
}
int vis[maxn],dis[maxn],pre[maxn],flow[maxn];
bool bfs()
{
	memset(dis,inf,sizeof(dis));
	memset(vis,0,sizeof(vis));
	memset(flow,inf,sizeof(flow));
	queue<int> q;
	q.push(s); vis[s] = 1; dis[s] = 0;
	while(!q.empty())
	{
		int fa = q.front(); q.pop(); vis[fa] = 0;
		for(int i = head[fa]; ~i; i = e[i].nxt)
		{
			int to = e[i].to, f = e[i].f, w = e[i].w;
			if(f && dis[to] > dis[fa]+w)
			{
				dis[to] = dis[fa]+w;
				flow[to] = min(flow[fa],f);
				pre[to] = i;
				if(!vis[to]) q.push(to), vis[to] = 1;
			}
		}
	}
	return dis[t] != 0x7f7f7f7f;
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		memset(head,-1,sizeof(head)); tot = 0;
		s = 0, t = n + 1;
		
		for(int i = 0,u,v,w; i < m; ++i)
		{
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,1,w);add(v,u,1,w);
		}
		add(s,1,2,0);  add(n,t,2,0);
		int ans = 0;
		while(bfs())
		{
			ans += dis[t]*flow[t];
			int x = t;
			while(x != s)
			{
				
				e[pre[x]].f-=flow[t]; e[pre[x]^1].f+=flow[t];
				x = e[pre[x]^1].to;
			}
		}
		printf("%d\n",ans);
	}
}