Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 31882   Accepted: 8445

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

解题思路

题意:找一条从 1 到 n 的道路,使得这条道路的每段距离中的最小值最大。
思路:利用Kruskal的思想,把每段道路的权值按照从大到小排序,然后依次加边直至 1 可达 n为止,此时这条路中最小直即为答案。
利用dijkstra或是spfa的思想去解,dis[ x ]表示从 1 到达 x 的道路中,其道路段的最小值的最大值。
 

Kruskal()

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1005;
struct Edge{
	int u,v,w;
}edge[maxn*maxn/2];
int fa[maxn];

bool cmp(Edge a,Edge b)
{
	return a.w > b.w;
}

int find(int x)
{
	return fa[x] == x?fa[x]:fa[x] = find(fa[x]);
}

void Union(int x,int y)
{
	int fx = find(x),fy = find(y);
	if (fx != fy)	fa[fx] = fy;
}

int main()
{
	int tcase;
	scanf("%d",&tcase);
	for (int t = 1;t <= tcase;t++)
	{
		int n,m,res = 0x3f3f3f3f;
		scanf("%d%d",&n,&m);
		for (int i = 0;i <= n;i++)	fa[i] = i;
		for (int i = 0;i < m;i++)
		{
			scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
		}
		sort(edge,edge + m,cmp);
		for (int i = 0; i< m;i++)
		{
			Union(edge[i].u,edge[i].v);
			if (find(1) == find(n))
			{
				res = edge[i].w;
				break;
			}
		}
		printf("Scenario #%d:\n",t);
		printf("%d\n",res);
		if (t != tcase)	printf("\n");
	}
	return 0;
} 

dijkstra()

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
struct Edge{
	int u,v,w,nxt;
	bool operator < (const Edge &a)const
	{
		return w < a.w;
	}
}edge[maxn*maxn];
int tot = 0,head[maxn],dis[maxn];
bool vis[maxn];

void addedge(int u,int v,int w)
{
	edge[tot] = (Edge){u,v,w,head[u]
	};
	head[u] = tot++;
}

void dijkstra(int st,int n)
{
	Edge p;
	priority_queue<Edge>que;
	memset(dis,0,sizeof(dis));
	memset(vis,false,sizeof(vis));
	p.v = st;
	p.w = INF;
	dis[st] = INF;
	que.push(p);
	while (!que.empty())
	{
		p = que.top();
		que.pop();
		int u = p.v;
		if (vis[u])	continue;
		vis[u] = true;
		for (int i = head[u];~i;i = edge[i].nxt)
		{
			int v = edge[i].v,w = edge[i].w;
			if (dis[v] < min(dis[u],w))
			{
				dis[v] = min(dis[u],w);
				p.v = v,p.w = dis[v];
				que.push(p);
			}
		}
	}
}

int main()
{
	int tcase;
	scanf("%d",&tcase);
	for (int t = 1;t <= tcase;t++)
	{
		int n,m,u,v,w;
		memset(head,-1,sizeof(head));
		scanf("%d%d",&n,&m);
		for (int i = 0;i < m;i++)
		{
			scanf("%d%d%d",&u,&v,&w);
			addedge(u,v,w);
			addedge(v,u,w);
		}
		dijkstra(1,n);
		printf("Scenario #%d:\n",t);
		printf("%d\n",dis[n]);
		if (t != tcase)	printf("\n");
	}
	return 0;

} 

spfa()

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
struct Edge{
	int u,v,w,nxt;
}edge[maxn*maxn];
int tot = 0,head[maxn],dis[maxn];
bool vis[maxn];

void addedge(int u,int v,int w)
{
	edge[tot] = (Edge){u,v,w,head[u]
	};
	head[u] = tot++;
}

void spfa(int st,int ed)
{
	memset(vis,false,sizeof(vis));
	memset(dis,0,sizeof(dis));
	queue<int>que;
	dis[st] = INF;
	que.push(st);
	vis[st] = true;
	while (!que.empty())
	{
		int u = que.front();
		que.pop();
		vis[u] = false;
		for (int i = head[u];~i;i = edge[i].nxt)
		{
			int v = edge[i].v,w = edge[i].w;
			if (min(dis[u],w) > dis[v])
			{
				dis[v] = min(dis[u],w);
				if (!vis[v])
				{
					que.push(v);
					vis[v] = true;
				}
			}
		}
	}
}

int main()
{
	int tcase;
	scanf("%d",&tcase);
	for (int t = 1;t <= tcase;t++)
	{
		int n,m,u,v,w;
		memset(head,-1,sizeof(head));
		scanf("%d%d",&n,&m);
		for (int i = 0;i < m;i++)
		{
			scanf("%d%d%d",&u,&v,&w);
			addedge(u,v,w);
			addedge(v,u,w);
		}
		spfa(1,n);
		printf("Scenario #%d:\n",t);
		printf("%d\n",dis[n]);
		if (t != tcase)	printf("\n");
	}
}

  

POJ 1797 Heavy Transportation(最大生成树/最短路变形)的更多相关文章

  1. POJ 1797 Heavy Transportation (最大生成树)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

  2. POJ - 1797 Heavy Transportation 单源最短路

    思路:d(i)表示到达节点i的最大能运输的重量,转移方程d(i) = min(d(u), limit(u, i));注意优先队列应该以重量降序排序来重载小于符号. AC代码 #include < ...

  3. poj 1797 Heavy Transportation(最大生成树)

    poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...

  4. POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...

  5. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  6. POJ 1797 Heavy Transportation

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  7. POJ 1797 Heavy Transportation SPFA变形

    原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  8. POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

  9. POJ 1797 Heavy Transportation (最短路)

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 22440   Accepted:  ...

随机推荐

  1. 谈一谈前端多容器(多webview平台)处理方案

    文中是我个人的一些开发经验,希望对各位有用,也希望各位多多支持讨论,指出文中不足以及提出您的一些建议. 双容器 得益于近几年移动端的发展,前端早已今非昔比,从大型框架来说angularJS.react ...

  2. SAP CRM 使用Javascript触发SAP Server Event

    原文地址:How To Trigger SAP Server Event With Javascript 本文地址:http://www.cnblogs.com/hhelibeb/p/5977921. ...

  3. ip命令和ifconfig命令(转载)

    Linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  4. 3D坦克大战游戏源码

    3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...

  5. iOS版打地鼠游戏源码

    打地鼠游戏源码,游戏是一款多关卡基于cocos2d的iPad打地鼠游戏源码,这也是一款高质量的打地鼠游戏源码,可以拥有逐步上升的关卡的设置,大家可以在关卡时设置一些商业化的模式来盈利的,非常完美的一款 ...

  6. CAS Client集群环境的Session问题及解决方案

    [原创申明:文章为原创,欢迎非盈利性转载,但转载必须注明来源] 之前写过一篇文章,介绍单点登录的基本原理.这篇文章重点介绍开源单点登录系统CAS的登录和注销的实现方法.并结合实际工作中碰到的问题,探讨 ...

  7. .htaccess添加Header set Cache-Control报错500

    在优化网站开启站点的图片缓存时,需要在.htaccess文件中加入: #文件缓存时间配置10分钟 <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf ...

  8. C语言运算符优先级

    优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 -- () 圆括号 (表达式)/函数名(形参表) -- . 成员选择(对象) 对象.成员名 -- ...

  9. 使用Logstash进行日志分析

    LogStash主要用于数据收集和分析方面,配合Elasticsearch,Kibana用起来很方便,安装教程google出来很多. 推荐阅读 Elasticsearch 权威指南 精通 Elasti ...

  10. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...