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. threadpool 的配置实用

    //spring mvc文件中的配置 <!-- ThreadPoolExecutor --> <bean id="threadPoolTaskExecutor" ...

  2. 《JavaScript面向对象编程指南》译者序

    相对于Perl.Python等动态脚本语言来说,JavaScript确实是一门饱受误解的语言.对于译者这种从20世纪90年代末走过来的C++程序员来说,尤其如此.在那个年代,提起JavaScript总 ...

  3. CSS3实现背景颜色渐变

    CSS3渐变色生成网站:http://gradients.glrzad.com/ 本文参考:前端设计之用CSS3做线性渐变效果http://webskys.com/css3/10.html 在CSS3 ...

  4. mysql-创建库之问题

    一.在登入mysql命令行,创建数据库时报错 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'xiaolu ...

  5. 在线程中用 OracleBulkCopy 导至 CPU 百分百

    抓取到的数据, 要批量写数据到 ORACLE , 一开始是用的EF, 处理速度很慢. 主要表现在验证数据上(db.GetValidationErrors), 每分钟才能写 1000条不到. 换成 En ...

  6. 27个知名企业品牌VI视觉识别系统规范手册

    Apple公司视觉设计规范 微软公司VI视觉系统 星巴克企业视觉规范手册 DELL品牌VI视觉手册 MTRADING品牌视觉规范 KFC视觉设计规范手册 麦当劳视觉规范 LEGO乐高玩具的品牌视觉规范 ...

  7. SoftEnther VPN 在Window的使用

    1.首先下载SoftEnther VPN Client 下载地址 2. 下载后,执行vpngate-client-×××.exe 文件 选择安装一个软件部分: SoftEnther VPN Clien ...

  8. google protocol buffer 使用说明

    一:编译源码 下载地址:http://code.google.com/p/protobuf/downloads/list 下载后,根据编译说明进行编译. windows 平台,直接打开msvc中的工程 ...

  9. 关于@Html.Action()的异常“控制器或该控制器未实现 IController。”

    解决之前: @Html.Action("BottomHelp", "Articles", new { num = 5}) 解决之后: @Html.Action( ...

  10. NDK SO 库开发与使用中的 ABI 构架选择

    Bugtags V1.2.7 引入了 NDK SO 库,在集成的时候,遇到不同的 SO 库打包到 APK 时,安装在某些机器上,出现 java.lang.UnsatisfiedLinkError 加载 ...