Heavy Transportation

POJ-1797

  • 这题是最短路题型的变形,该题不是求起点到终点的最短路,而是求路径中的最小边的最大值。
  • 这题的求解思路是:将原来dijkstra中的松弛方程改一下,改成求最小边的最大值的松弛方程:d[j]=max(d[j],min(d[i],w[i][j]))。
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#define MOD 1e9+7
using namespace std;
int e[1005][1005];
int dis[1005];
bool vis[1005];
int main()
{
int T,n,m,ca=0;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
memset(e,0,sizeof(e));
int a,b,w;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&w);
e[a][b]=e[b][a]=w;
}
dis[1]=INF;
for(int i=2;i<=n;i++)
dis[i]=e[1][i];
memset(vis,false,sizeof(vis));
for(int k=0;k<n;k++)
{
int mi,mm=-INF;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&dis[i]>mm)
{
mm=dis[i];
mi=i;
}
}
vis[mi]=true;
for(int i=1;i<=n;i++)
{
if(!vis[i])
dis[i]=max(dis[i],min(dis[mi],e[mi][i]));
}
}
printf("Scenario #%d:\n%d\n\n",++ca,dis[n]);
}
return 0;
}

java:

package POJ;
import java.util.*;
public class POJ_1797 {
private static int n,m;//n:1-1000
static int t;//case
static int [][]w;
static int []dis;
static boolean []flag;
static final int INF=0X3F3F3F3F;
static int s,e;
static int dijkstra() {
for(int i=0;i<n;i++) {
int index=-1,mins=INF;
for(int j=0;j<n;j++) {
if(!flag[j]&&dis[j]>mins) {
mins=dis[index=j];
}
}
if(index==-1)
break;
flag[index]=true;
for(int j=0;j<n;j++) {
if(!flag[j])
dis[j]=Math.max(dis[j],Math.min(dis[index],w[index][j]));
}
}
return dis[e];
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
t=cin.nextInt();
int cnt=1;
while(t>0) {
n=cin.nextInt();
m=cin.nextInt();
w=new int[n][n];
dis=new int[n];
for(int i=0;i<m;i++) {
int from,to;
from=cin.nextInt();
to=cin.nextInt();
w[from-1][to-1]=w[to-1][from-1]=cin.nextInt();
}
s=0;e=n-1;
dis[s]=INF;
flag=new boolean[n];
for(int i=1;i<n;i++) {
dis[i]=w[0][i];
}
flag[s]=true;
System.out.println("Scenario #"+cnt+":");
System.out.println(dijkstra());
t--;
cnt++;
}
} }

POJ-1797(最短路变形-dijkstra)的更多相关文章

  1. poj 1797 最短路变形dijkstra

    题意:题目大意:有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量 链接:点我 解题思路:其实这个求最大边可以近似于 ...

  2. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  3. poj 1797(最短路变形)

    题目链接:http://poj.org/problem?id=1797 思路:题目意思很简单,n个顶点,m条路,每条路上都有最大载重限制,问1->n最大载重量.其实就是一最短路的变形,定义wei ...

  4. POJ 1797 最短路变形所有路径最小边的最大值

    题意:卡车从路上经过,给出顶点 n , 边数 m,然后是a点到b点的权值w(a到b路段的承重),求卡车最重的重量是多少可以从上面经过. 思路:求所有路径中的最小的边的最大值.可以用迪杰斯特拉算法,只需 ...

  5. POJ 1797 Heavy Transportation (Dijkstra变形)

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

  6. POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)

    题目链接: http://poj.org/problem?id=1797 Background Hugo Heavy is happy. After the breakdown of the Carg ...

  7. POJ 1797 Heavy Transportation(Dijkstra)

    http://poj.org/problem?id=1797 题意 :给出N个城市M条边,每条边都有容量值,求一条运输路线使城市1到N的运输量最大. 思路 :用dijkstra对松弛条件进行变形.解释 ...

  8. POJ 1797 Heavy Transportation (dijkstra 最小边最大)

    Heavy Transportation 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Backgro ...

  9. poj 3013 最短路变形

    http://poj.org/problem?id=3013 给出n个点,m个边.给出每个点的权值,每个边的权值.在m条边中选n-1条边使这n个点成为一棵树,root=1,求这棵树的最小费用,费用=树 ...

随机推荐

  1. hdu4339 Query

    Problem Description You are given two strings s1[0..l1], s2[0..l2] and Q - number of queries. Your t ...

  2. anaconda python3.7 安装 tensorflow-gpu 2.0.0 beta1 配置PyCharm

    参考tensorflow 公众号<tensorflow2.0 安装指南> https://mp.weixin.qq.com/s/7rNXFEC5HYe91RJ0-9CKdQ # 1. NV ...

  3. codefroces 7C

    C. Line time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  4. MATLAB中将mat文件转为txt格式文件

    直接保存为txt文件: 可以用fprintf函数,来代替save函数 比如现在我有一个变量a=[0.1223   345.4544] 如果我想保存它的话,可以用下面的程序: fid = fopen(' ...

  5. WSL2+Terminal+VScode配置调试

    最近几天一直想找个方法把VMware虚拟机和远程连接工具MobaXterm这一组配合替换掉,因为每次开启虚拟机操作Ubuntu都需要占用很大的内存,而且要等好久好久才能开启!!!后面还要使用MobaX ...

  6. how to close macos eject icon from menu bar

    how to close macOS eject icon from the menu bar close eject https://apple.stackexchange.com/question ...

  7. SVG namespace & preview bug

    SVG namespace & preview bug error This XML file does not appear to have any style information as ...

  8. asm movbe 指令

    movbe MOVBE 目标操作数,源操作数 复制源操作数的数据,交换字节后,移动数据 假如: movbe eax,(float)1000.0 eax == 0x00007A44 movbe eax, ...

  9. html2Canvas to Images

    <script> $(function () { var content = document.getElementById("shareImages"); conte ...

  10. 为什么建议选择居住在墨尔本CBD以南2公里内

    英国房地产公司PAUL ADAMS ARCHITECT ,简称PPA(公司编号:07635831)成立于2011年,是一家成立近十年的老牌房地产公司.PAA公司一直有着自己的房地产理念,秉持房子是用来 ...