POJ 1797 Heavy Transportation(最大生成树/最短路变形)
| Time Limit: 3000MS | Memory Limit: 30000K | |
| Total Submissions: 31882 | Accepted: 8445 |
Description
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
Output
Sample Input
1 3 3 1 2 3 1 3 4 2 3 5
Sample Output
Scenario #1: 4
解题思路
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(最大生成树/最短路变形)的更多相关文章
- POJ 1797 Heavy Transportation (最大生成树)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- POJ - 1797 Heavy Transportation 单源最短路
思路:d(i)表示到达节点i的最大能运输的重量,转移方程d(i) = min(d(u), limit(u, i));注意优先队列应该以重量降序排序来重载小于符号. AC代码 #include < ...
- poj 1797 Heavy Transportation(最大生成树)
poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...
- POJ.1797 Heavy Transportation (Dijkstra变形)
POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...
- POJ 1797 Heavy Transportation
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- POJ 1797 Heavy Transportation SPFA变形
原题链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- POJ 1797 ——Heavy Transportation——————【最短路、Dijkstra、最短边最大化】
Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64 ...
- POJ 1797 Heavy Transportation (最短路)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 22440 Accepted: ...
随机推荐
- 使用 Web API 模拟其他用户
模拟的要求 模拟可代表另一个 Microsoft Dynamics CRM 用户,用于执行业务逻辑(代码)以便提供所需功能或服务,它使用模拟用户的相应角色和基于对象的安全性.这项技术很有必要,因为 M ...
- listview侧滑删除
自定义Listview,向左滑动,右边刚好显示删除按钮: public class SlideListView extends ListView { private int mScreenWidth; ...
- RAC textView的双向绑定
今天在写关于textView的数据绑定时原先写法是这样的: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78 ...
- Database first with EntityFramework (Migration)安装和升级
最近看了国外几个项目,发现用EntityFramework做Code First的项目现在很流行. 最让我有兴趣的一个功能则是,EntityFramework对于数据库的安装和升级的无缝完美支持,且很 ...
- [Erlang 0109] From Elixir to Erlang Code
Elixir代码最终编译成为erlang代码,这个过程是怎样的?本文通过一个小测试做下探索. 编译一旦完成,你就看到了真相 Elixir代码组织方式一方面和Erlang一样才用非常 ...
- Squirrel: 通用SQL、NoSQL客户端
安装 配置数据库 配置驱动 配置连接 如果你的工作中,需要使用到多个数据库,又不想在多种客户端之间切换来切换去.那么就需要找一款支持多数据库的客户端工具了.如果你要连接多个关系型数据库,你就可以使用N ...
- Spark概述
背景 目前按照大数据处理类型来分大致可以分为:批量数据处理.交互式数据查询.实时数据流处理,这三种数据处理方式对应的业务场景也都不一样: 关注大数据处理的应该都知道Hadoop,而Hadoop的核心为 ...
- windows下redis安装
最近因公司项目原因,去了趟昆明出差,其中第一次接触安装redis,配置sentinel,学习到不少,但也都是皮毛而已,本随笔记下所学知识. 1.首先介绍下redis,来源自百度百科 redis是一个k ...
- TCP十一种状态
2.全部11种状态 2.1.客户端独有的:(1)SYN_SENT (2)FIN_WAIT1 (3)FIN_WAIT2 (4)CLOSING (5)TIME_WAIT . 2.2.服务器独有的:(1)L ...
- js中判断对象具体类型
大家可能知道js中判断对象类型可以用typeof来判断.看下面的情况 <script> alert(typeof 1);//number alert(typeof "2" ...