Heavy Transportation
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 26442   Accepted: 7044

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的所有每个路径中的最小承载量中的最大值(与:所有路径中最长边的最短值相区别)。
/*
dijkstra Accepted 4316 KB 391 ms
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V,E; int dijkstra(int s)
{
int vis[MAXN];
memset(vis,,sizeof(vis));
int d[MAXN];//代表最大承载量
for(int i=;i<=V;i++) d[i]=mp[s][i]; int n=V;
while(n--)
{
int maxcost,k;
maxcost=;
for(int i=;i<=V;i++)
{
if(!vis[i]&&d[i]>maxcost)
{
k=i;
maxcost=d[i];
}
} vis[k]=;
for(int i=;i<=V;i++)
{
if(!vis[i]&&d[i]<min(d[k],mp[k][i]))
{
d[i]=min(d[k],mp[k][i]);
}
}
}
return d[V];
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
memset(mp,,sizeof(mp));//承载量初始化为0
scanf("%d%d",&V,&E);
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
mp[u][v]=mp[v][u]=cost;
} printf("Scenario #%d:\n",cas);
printf("%d\n\n",dijkstra());
} return ;
}
/*
floyd Time Limit Exceeded
*/
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
int V,E;
scanf("%d%d",&V,&E);
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
if(i==j) mp[i][j]=;
else mp[i][j]=INF;
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
mp[u][v]=mp[v][u]=cost;
} for(int k=;k<=V;k++)
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
if(mp[i][k]<mp[i][j]&&mp[k][j]<mp[i][j])
mp[i][j]=max(mp[i][k],mp[k][j]); printf("Scenario #%d:\n",cas);
printf("%d\n\n",mp[][V]);
} return ;
}
/*
堆优化dijkstra 1797 Accepted 5224K 329MS
*/
#include"cstdio"
#include"queue"
#include"vector"
#include"algorithm"
#include"cstring"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,cost;
Edge(int to,int cost)
{
this->to=to;
this->cost=cost;
}
friend bool operator<(const Edge &a,const Edge &b)
{
return a.cost < b.cost;
}
};
int V,E;
vector<int> G[MAXN];
int mp[MAXN][MAXN];
int dijkstra(int s)
{
int d[MAXN];
priority_queue<Edge> que; for(int i=;i<=V;i++)
{
que.push(Edge(i,mp[s][i]));
d[i]=mp[s][i];
}
while(!que.empty())
{
Edge e=que.top();que.pop();
if(e.to==V) return e.cost; int v=e.to;
if(d[v]>e.cost) continue;
for(int i=;i<G[v].size();i++)
{
int u=G[v][i];
if(d[u]<min(d[v],mp[v][u]))
{
d[u]=min(d[v],mp[v][u]);
que.push(Edge(u,d[u]));
}
}
}
return -;
}
int main()
{ int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
memset(mp,,sizeof(mp));
scanf("%d%d",&V,&E);
for(int i=;i<=V;i++)
{
G[i].clear();
}
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
G[u].push_back(v);
G[v].push_back(u);
mp[u][v]=mp[v][u]=cost;
}
printf("Scenario #%d:\n",cas);
printf("%d\n\n",dijkstra());
} return ;
}

二分+bfs

/*
1797 Accepted 1420K 329MS C++
*/
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
struct Edge{
int to,w;
Edge(){}
Edge(int to,int w)
{
this->to=to;
this->w=w;
}
};
int n,m;
vector<Edge> arc[MAXN];
int src,ter,vis[MAXN];
bool bfs(int limit)
{
memset(vis,,sizeof(vis));
queue<int> que;
que.push(src);
vis[src]=;
while(!que.empty())
{
int u=que.front();que.pop();
if(u==ter) return true;
for(int i=,size=arc[u].size();i<size;i++)
{
Edge e=arc[u][i];
if(!vis[e.to]&&limit<=e.w)
{
vis[e.to]=;
que.push(e.to);
}
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) arc[i].clear();
src=;
ter=n;
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
arc[u].push_back(Edge(v,w));
arc[v].push_back(Edge(u,w));
}
int l=,r=INF;
while(r-l>)
{
int mid=(l+r)/;
if(bfs(mid))
{
l=mid;
}
else
{
r=mid;
}
}
printf("Scenario #%d:\n",cas);
printf("%d\n\n",l);
}
return ;
}

POJ1797(dijkstra求最短最长边)的更多相关文章

  1. ACM - 最短路 - AcWing 849 Dijkstra求最短路 I

    AcWing 849 Dijkstra求最短路 I 题解 以此题为例介绍一下图论中的最短路算法.先让我们考虑以下问题: 给定一个 \(n\) 个点 \(m\) 条边的有向图(无向图),图中可能存在重边 ...

  2. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  3. 逆FizzBuzz问题求最短序列

    问题描述 FizzBuzz问题:一个大于0的自然数能整除3,将输出“Fizz”:能整除5,将输出“Buzz”:能整除3和5,将输出“FizzBuzz”:否则输出自己. 逆FizzBuzz问题最短序列: ...

  4. CF 427D Match &amp; Catch 求最短唯一连续LCS

    题目来源:CF 427D Match & Catch 题意:给出2个字符串 求最短的连续的公共字符串 而且该字符串在原串中仅仅出现一次 思路:把2个字符串合并起来求height 后缀数组hei ...

  5. The Super Powers UVA 11752 分析分析 求无符号长整形以内的数满足至少可以用两种不同的次方来表示。比如64 = 2^6 = 8^2; 一个数的1次方不算数。

    /** 题目:The Super Powers UVA 11752 链接:https://vjudge.net/contest/154246#problem/Y 题意:求无符号长整形以内的数满足至少可 ...

  6. 关于dijkstra求最短路(模板)

    嗯....   dijkstra是求最短路的一种算法(废话,思维含量较低,   并且时间复杂度较为稳定,为O(n^2),   但是注意:!!!!         不能处理边权为负的情况(但SPFA可以 ...

  7. Aizu-2249 Road Construction(dijkstra求最短路)

    Aizu - 2249 题意:国王本来有一个铺路计划,后来发现太贵了,决定删除计划中的某些边,但是有2个原则,1:所有的城市必须能达到. 2:城市与首都(1号城市)之间的最小距离不能变大. 并且在这2 ...

  8. 牛客小白月赛6 C 桃花 dfs 求树上最长直径

    链接:https://www.nowcoder.com/acm/contest/136/C来源:牛客网 题目描述 桃花一簇开无主,可爱深红映浅红.                            ...

  9. 状压dp,松鼠从起点出发,拿到所有坚果,然后返回起点,求最短时间。

    UVA10944 松鼠从起点出发,拿到所有坚果,然后返回起点,求最短时间. #include<iostream> #include<cstdio> #include<al ...

随机推荐

  1. HDFS源码分析数据块汇报之损坏数据块检测checkReplicaCorrupt()

    无论是第一次,还是之后的每次数据块汇报,名字名字节点都会对汇报上来的数据块进行检测,看看其是否为损坏的数据块.那么,损坏数据块是如何被检测的呢?本文,我们将研究下损坏数据块检测的checkReplic ...

  2. Ubuntu 14.04lts安装vncserver

    之前有在centos上安装过非常多次vncserver,也写过一个centos 7上的安装文档.近来常识了好几次在ubuntu上安装都没有成功,这次最终搞定了.ubuntu自带的桌面是unity.这个 ...

  3. 06 php 单例模式

    一:单例模式的三大原则 (1)构造函数需要标记为非public(防止外部使用new操作符创建对象),单例类不能在其他类中实例化,只能被自身实例化. (2)拥有一个保存类的实例的静态成员变量$_inst ...

  4. jquery实现重置

    $('#reset').click(function(){ $('#info_frm')[0].reset(); });

  5. eclipse转到IntelliJ IDEA 2017.1入坑指南

    最近准备从eclipse转到IDE上去,由于eclipse占用的内存太大,而且IDE看着逼格还是比较大的,在转移项目的时候遇到好多的坑呀!在这里记录一下 关于:2017.1版本 之前装的是2016的版 ...

  6. 九度OJ 1156:谁是你的潜在朋友 (并查集)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5802 解决:2593 题目描述: "臭味相投"--这是我们描述朋友时喜欢用的词汇.两个人是朋友通常意味着他们存在着许多 ...

  7. 题解 CF97C 【Winning Strategy】

    题解 CF97C [Winning Strategy] 此题是某平台%你赛原题,跟大家分享一下某校zsy和sxr等同学的神仙做法. 我解释一下题意,大是说,我有[无限]个人,每个人可以对他" ...

  8. Java反射详解(转)

    原文地址:http://www.importnew.com/17616.html 动态语言 动态语言,是指程序在运行时可以改变其结构:新的函数可以被引进,已有的函数可以被删除等在结构上的变化.比如众所 ...

  9. 私有 npm 仓库的搭建

    cnpm 是企业内部搭建 npm 镜像和私有 npm 仓库的开源方案,当企业业务逻辑相关的模块可能不适合开源.这部分私有的模块就可以放在私有 npm 仓库中来管理和维护. 以下为搭建私有 npm 的详 ...

  10. git创建与管理远程分支【转】

    本文转载自:http://blog.chinaunix.net/uid-9398085-id-3164754.html git创建与管理远程分支 1.远程分支就是本地分支push到服务器上的时候产生的 ...