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 ;
}

hdu 1797 靠谱的算法应该是最大生成树,但是本人用最大流做的的更多相关文章

  1. 谱聚类算法(Spectral Clustering)

        谱聚类(Spectral Clustering, SC)是一种基于图论的聚类方法--将带权无向图划分为两个或两个以上的最优子图,使子图内部尽量相似,而子图间距离尽量距离较远,以达到常见的聚类的 ...

  2. hdu 1269 迷宫城堡(Targin算法)

    ---恢复内容开始--- 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. 算法提高 最小方差生成树(Kruskal)_模板

     算法提高 最小方差生成树   时间限制:1.0s   内存限制:256.0MB        问题描述 给定带权无向图,求出一颗方差最小的生成树. 输入格式 输入多组测试数据.第一行为N,M,依次是 ...

  4. ACM: HDU 3790 最短路径问题-Dijkstra算法

    HDU 3790 最短路径问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  5. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  6. HDU 2066 最短路floyd算法+优化

    http://acm.hdu.edu.cn/showproblem.php?pid=206 题意 从任意一个邻居家出发 到达任意一个终点的 最小距离 解析 求多源最短路 我想到的是Floyd算法 但是 ...

  7. 谱聚类算法(Spectral Clustering)优化与扩展

    谱聚类(Spectral Clustering, SC)在前面的博文中已经详述,是一种基于图论的聚类方法,简单形象且理论基础充分,在社交网络中广泛应用.本文将讲述进一步扩展其应用场景:首先是User- ...

  8. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...

  9. HDU 5289 Assignment (ST算法区间最值+二分)

    题目链接:pid=5289">http://acm.hdu.edu.cn/showproblem.php?pid=5289 题面: Assignment Time Limit: 400 ...

随机推荐

  1. xml json protobuf

    本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background xml,json,protobuf都是格式化手段,喜欢哪个,会用哪个,该用哪个,用哪个? 随 ...

  2. opencv配置(2.49)

    转载自浅墨大神http://blog.csdn.net/poem_qianmo/article/details/19809337 OpenCV2.4.9和2.4.8的配置几乎一样,唯一的区别在下文中的 ...

  3. Linux下获得系统时间的C语言实现

    Linux下获得系统时间的C语言的实现方法 #include<time.h> //C语言的头文件#include<stdio.h> //C语言的I/O   int main() ...

  4. Mac OS X 上Lua的安装方法

    先在Mac OS的终端查询下本机是否已安装Lua Last login: Thu Jul 10 07:54:48 on ttys000 keshans-Mac-mini:~ keshan$ lua - ...

  5. CAD的API们

    AutoCAD有4种API,.net,lisp,activex和ObjectARX(C++).它们都是用来给cad写插件什么的,依赖cad运行. 另有一个RealDWG SDK,这是用来读写dwg或d ...

  6. Alpha版本——Postmortem会议

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 设想和目标 1.我们的软件要解决什么 ...

  7. [转]Windows多进程编程

    转自:http://blog.csdn.net/bxhj3014/article/details/2082255 一.进程的概念       进程是是一个正在运行的程序的实例(飘---),是系统分配资 ...

  8. Centos 安装vsftpd 服务器

    一:检查有没有安装vsftpd 二:安装vsftpd 三:安装之后重启 四:修改vsftpd配置文件 配置文件路径在/etc/vsftpd目录下 默认是注释掉的,把#号去掉 然后重启vsftpd 五: ...

  9. Shiro 整合SpringMVC 并且实现权限管理

    Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大 ...

  10. makefile自动生成依赖关系

    手工编写依赖关系不仅工作量大而且极易出现遗漏,更新也很难及时,修改源或头文件后makefile可能忘记修改.为了解决这个问题,可以用gcc的-M选项自动生成目标文件和源文件的依赖关系.-M选项会把包含 ...