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
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = +;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int dp[][],dis[maxn];
int t,n,m,u,v,w;
int x[maxn],y[maxn],vis[maxn];
int ca;
void dij()
{
ms(vis,);
rep(i,,n) dis[i]=dp[][i];
dis[]=;
vis[]=;
for(int i=;i<n;i++) //
{
int Max=,k=;
rep(j,,n)
if(!vis[j] && dis[j]>Max)
Max=dis[k=j];
vis[k]=;
rep(j,,n)
if(!vis[j] && dis[j]<min(dis[k], dp[k][j]))
dis[j]=min(dis[k],dp[k][j]);
}
}
int main()
{
ca=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
ms(vis,);
ms(dp,);
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
dp[u][v]=dp[v][u]=w;
}
dij();
printf("Scenario #%d:\n%d\n\n",ca++,dis[n]);
}
}

dij

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5+;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int fa[maxn],dis[maxn];
int t,n,m,u,v,w;
int vis[maxn];
int ca;
struct node
{
int u,v,w;
bool operator < (const node &x) const{
return w>x.w;
}
}e[maxn<<];
int Find(int x)
{
return x==fa[x]?x:Find(fa[x]);
} int main()
{
ca=;
scanf("%d",&t);
while(t--)
{
int ans=INF;
scanf("%d%d",&n,&m);
rep(i,,n) fa[i]=i;
rep(i,,m)
{
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
}
sort(e+,e+m+);
rep(i,,m)
{
int fx=Find(e[i].u);
int fy=Find(e[i].v);
if(Find()!=Find(n)) //源汇点不在同一连通分量就一直加边
{
ans=e[i].w;
fa[fx]=fy;
}
else break; //起点和终点一旦连通那么解就是这条边了
}
printf("Scenario #%d:\n%d\n\n",ca++,ans);
}
}

kruskal

最新文章

  1. Card Flip
  2. python3 购物程序
  3. ooad单例模式-Singleton
  4. SharePoint Client Add Folder,file to Library
  5. js函数、表单验证
  6. maven jetty运行命令
  7. MyBatis总结-实现关联表查询
  8. loadrunner:关联操作
  9. 【转】WEB网站常见受攻击方式及解决办法
  10. noip提高组2011 Mayan游戏
  11. 超链接(空链接-target-title属性)
  12. Python小白学习之路(九)—【字符串格式化】【百分号方式】【format方式】
  13. 结合after使用content
  14. GDI+(一):GDI+ 绘图基础
  15. 3D 相关
  16. (转)Android DiskLruCache完全解析,硬盘缓存的最佳方案
  17. Voting and Shuffling to Optimize Atomic Operations
  18. Exchange2010启用反垃圾邮件功能
  19. 关于logback
  20. 图像连通域检测的2路算法Code

热门文章

  1. Java 中 给静态方法 添加泛型 (static &lt;T&gt;)
  2. ASP.NET和ASP的区别是什么
  3. Codeforces 351B Jeff and Furik 概率 | DP
  4. 2015/8/29 Python基础(3):数值
  5. SourceTree for mac 注册过程(v2.7.6a)
  6. Item 5 避免创建不必要的对象
  7. 【51NOD】1096 距离之和最小
  8. NGINX: 配置 HSTS
  9. MSSQL数据库 &quot;无法删除数据库 &quot;***&quot;,因为该数据库当前正在使用&quot; 解决方案
  10. Javascript prototype 及 继承机制的设计思想