hdu 1797 靠谱的算法应该是最大生成树,但是本人用最大流做的

时间:2023-03-09 09:27:02
hdu 1797 靠谱的算法应该是最大生成树,但是本人用最大流做的
Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 22294   Accepted: 5916

Description

Background 
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem 
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

1
3 3
1 2 3
1 3 4
2 3 5

Sample Output

Scenario #1:
4

Source

附上代码:

 #include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<climits>
#define MAXE 1010*1010*2
#define MAXP 1010
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
using namespace std;
struct Edge
{
int s,t,f,next;
} edge[MAXE];
int head[MAXP];
int cur[MAXP];
int pre[MAXP];
int stack[MAXE];
int used[MAXP];
int ent;
int maxn;
int n,m,s,t;
int num;
void add(int start,int last,int f)
{
edge[ent].s=start;
edge[ent].t=last;
edge[ent].f=f;
edge[ent].next=head[start];
head[start]=ent++;
edge[ent].s=last;
edge[ent].t=start;
edge[ent].f=;
edge[ent].next=head[last];
head[last]=ent++;
}
bool bfs(int S,int T)
{
memset(pre,-,sizeof(pre));
pre[S]=;
queue<int>q;
q.push(S);
while(!q.empty())
{
int temp=q.front();
q.pop();
for(int i=head[temp]; i!=-; i=edge[i].next)
{
int temp2=edge[i].t;
if(pre[temp2]==-&&edge[i].f>maxn)
{
pre[temp2]=pre[temp]+;
q.push(temp2);
}
}
}
return pre[T]!=-;
}
void dinic(int start,int last)
{
int flow=,now;
maxn=;
while(bfs(start,last))
{
int top=;
memcpy(cur,head,sizeof(head));
int u=start;
while()
{
if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流
{
int minn=INT_MAX;
for(int i=; i<top; i++)
{
if(minn>edge[stack[i]].f)
{
minn=edge[stack[i]].f;
now=i;
}
}
maxn=Max(maxn,minn);
edge[stack[now]].f=edge[stack[now]^].f=;
top=now;
u=edge[stack[top]].s;
}
for(int i=cur[u]; i!=-; cur[u]=i=edge[i].next) //找出从u点出发能到的边
if(edge[i].f&&pre[edge[i].t]==pre[u]+)
break;
if(cur[u]==-)//如果从该点未找到可行边,将该点标记并回溯
{
if(top==)break;
pre[u]=-;
u=edge[stack[--top]].s;
}
else//如果找到了继续运行
{
stack[top++]=cur[u];
u=edge[cur[u]].t;
}
}
}
}
int main()
{
int cas;
cin>>cas;
int sum=;
while(cas--)
{
memset(head,-,sizeof(head));
ent=;
scanf("%d%d",&n,&m);
s=;t=n;
int u,v,flow;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&u,&v,&flow);
add(u,v,flow);
add(v,u,flow);
}
printf("Scenario #%d:\n",sum++);
dinic(s,t);
printf("%d\n\n",maxn);
}
return ;
}