HDU 5294(Tricks Device-最短路最小割)[Template:SPFA]

17 篇文章 0 订阅
11 篇文章 0 订阅

Tricks Device

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2389    Accepted Submission(s): 635


Problem Description
Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
 

Input
There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
 

Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
 

Sample Input
  
  
8 9 1 2 2 2 3 2 2 4 1 3 5 3 4 5 4 5 8 1 1 6 2 6 7 5 7 8 1
 

Sample Output
  
  
2 6
 

Author
FZUACM
 

Source
 

Recommend
 

最短路,最小割




#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (2000+10)
#define MAXM (60000*2+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

class Max_flow
{
public:
	int n,s,t;
	int q[MAXN];
	int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;
	void addedge(int u,int v,int w)
	{
		edge[++size]=v;
		weight[size]=w;
		next[size]=pre[u];	
		pre[u]=size;
	}
	void addedge2(int u,int v,int w){addedge(u,v,w);addedge(v,u,0);}
	bool b[MAXN];
	int d[MAXN];
	bool SPFA(int s,int t)
	{
		For(i,n) d[i]=INF;
		MEM(b)
		d[q[1]=s]=0; b[s]=1;
		int head=1,tail=1;
		while (head<=tail) {
			int now=q[head++];
			Forp(now) {
				int &v=edge[p];
				if (weight[p]&&!b[v])
				{
					d[v]=d[now]+1;
					b[v]=1,q[++tail]=v;
				}
			}
		} 
		return b[t];
	}
	int iter[MAXN];
	int dfs(int x,int f)
	{
		if (x==t) return f;
		Forpiter(x)
		{
			int v=edge[p];
			if (weight[p]&&d[x]<d[v]) 
			{
				int nowflow=dfs(v,min(weight[p],f));
				if (nowflow)
				{
					weight[p]-=nowflow;
					weight[p^1]+=nowflow;
					return nowflow;
				}
			}
		}
		return 0;
	} 
	int max_flow(int s,int t)
	{
		int flow=0;
		while (SPFA(s,t))
		{
			For(i,n) iter[i]=pre[i];
			int f;
			while (f=dfs(s,INF)) flow+=f;
		}
		return flow;
	}
	void mem(int _n,int _s,int _t) { n=_n,s=_s,t=_t; size=1; MEM(pre) }
}S; 
class SPFA
{
public:
	void mem()
	{
		MEM(pre) MEM(edge) MEM(pre) MEM(weight) size=1;
	}
	int q[MAXN*100];
	int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;
	void addedge(int u,int v,int w)
	{
		edge[++size]=v;
		weight[size]=w;
		next[size]=pre[u];	
		pre[u]=size;
	}
	void addedge2(int u,int v,int w){addedge(u,v,w);addedge(v,u,w);}
	int d[MAXN];
	bool b[MAXN];
	int spfa(int s,int t)
	{
		MEM(b) MEMI(d) 
		b[s]=1; d[s]=0;
		
		int head=1,tail=1;q[1]=1;
		while(head<=tail)
		{
			int now=q[head++];
			b[now]=0;
			Forp(now)
			{
				int v=edge[p];
				if (d[now]+weight[p]<d[v]) {
					d[v]=d[now]+weight[p];
					if (!b[v]) { b[v]=1,q[++tail]=v;
					} 
				}
			}
		}
		return d[t];
	}
}S1,S2;
class link_table
{
public:
	void mem()
	{
		MEM(pre) MEM(edge) MEM(pre) MEM(weight) size=1;
	}
	int q[MAXN*100];
	int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;
	
	void addedge(int u,int v,int w)
	{
		edge[++size]=v;
		weight[size]=w;
		next[size]=pre[u];	
		pre[u]=size;
	}
	void addedge2(int u,int v,int w){addedge(u,v,w);addedge(v,u,w);}
}St;
int n,m;
int a[MAXM],b[MAXM],l[MAXM];
int main()
{
//	freopen("G.in","r",stdin);
//	freopen(".out","w",stdout);
	while(scanf("%d%d",&n,&m)==2)
	{
		For(i,m) scanf("%d%d%d",&a[i],&b[i],&l[i]);
		S1.mem();
		For(i,m) {
			S1.addedge2(a[i],b[i],l[i]);
		}
		S1.spfa(1,n);
		
		S.mem(n,1,n);
		S2.mem();

		For(i,m) {
			if (S1.d[a[i]]+l[i]==S1.d[b[i]]) S.addedge2(a[i],b[i],1),S2.addedge(a[i],b[i],1);
			else if (S1.d[a[i]]-l[i]==S1.d[b[i]]) S.addedge2(b[i],a[i],1),S2.addedge(b[i],a[i],1);
		}
		
		int t=S.max_flow(1,n);
		cout<<t<<' ';
		cout<<m-S2.spfa(1,n)<<endl;
	}
	return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值