poj_2263 Heavy Cargo (Dijkstra)

news/2025/2/22 14:48:20

Heavy Cargo

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 2264

Accepted: 1252

题目链接:http://poj.org/problem?id=2263

Description

Big Johnsson Trucks Inc. is acompany specialized in manufacturing big trucks. Their latest model, theGodzilla V12, is so big that the amount of cargo you can transport with it isnever limited by the truck itself. It is only limited by the weight restrictionsthat apply for the roads along the path you want to drive.

Given start and destination city, your job is to determine the maximum load ofthe Godzilla V12 so that there still exists a path between the two specifiedcities.

Input

The input will contain one ormore test cases. The first line of each test case will contain two integers:the number of cities n (2<=n<=200) and the number of road segments r(1<=r<=19900) making up the street network.
Then r lines will follow, each one describing one road segment by naming thetwo cities connected by the segment and giving the weight limit for trucks thatuse this segment. Names are not longer than 30 characters and do not containwhite-space characters. Weight limits are integers in the range 0 - 10000.Roads can always be travelled in both directions.
The last line of the test case contains two city names: start and destination.
Input will be terminated by two values of 0 for n and r.

Output

For each test case, printthree lines:

  • a line saying "Scenario #x" where x is the     number of the test case
  • a line saying "y tons" where y is the     maximum possible load
  • a blank line

Sample Input

4 3

Karlsruhe Stuttgart 100

Stuttgart Ulm 80

Ulm Muenchen 120

Karlsruhe Muenchen

5 5

Karlsruhe Stuttgart 100

Stuttgart Ulm 80

Ulm Muenchen 120

Karlsruhe Hamburg 220

Hamburg Muenchen 170

Muenchen Karlsruhe

0 0

Sample Output

Scenario #1

80 tons

 

Scenario #2

170 tons

Source

UlmLocal 1998

解题思路:

         这道题目是求两点之间的路径的最小子路径中是最大值,根据输入可以将其转换成图,在用Dijkstra算法来算出单元最短路径,如果该条路径的最小值大于另外一条路径到该点的最小值,那么取两个最小值中的最大值,这样就可以求出起点到任意一点的值

代码:

 

#include <iostream>
#include<cstdio>
#include<cstring>
#define maxv 203
#define maxe 20000
#define VALUE 0xffffff
using namespace std;

struct Trie
{
    char str[21];
    int id;
};

Trie trie[maxv];
int g[maxv][maxv];
int visited[maxe];
int d[maxv];//源点到任意点的最短距离
int minValue[maxv];//源点到任意点最短距离的最小值
int n,m;
int num;//字典个数
//查找某个单词的id
int getTrieId(char *str)
{
    for(int i=0;i<num;i++)
    {
        if(strcmp(trie[i].str,str)==0)
            return trie[i].id;
    }    
    strcpy(trie[num].str,str);
    trie[num].id=num;
	num++;
    return num-1;
}

int maxVal(int a,int b)
{
	return a>b?a:b;
}

int minVal(int a,int b)
{
	return a<b?a:b;
}

void dijkstra(int s,int e)
{
	int i;
	for(i=0;i<=n;i++)
	{
		visited[i]=0;
		d[i]=VALUE;
		minValue[i]=VALUE;
	}
	d[s]=0;
	while(true)
	{
		int t=-1;
		for(i=0;i<n;i++)
		{
			if(!visited[i] && (t==-1 || d[i]<d[t]))
				t=i;
		}
		if(t==-1)
			break;
		visited[t]=1;

		for(i=0;i<n;i++)
		{
			if((d[i]>=d[t]+g[i][t]) && g[i][t]!=0)
			{
				d[i]=d[t]+g[i][t];
				if(minValue[i]!=VALUE)
				{
					minValue[i]=maxVal(minValue[i],minVal(minValue[t],g[i][t]));
				}
				else
				{
					minValue[i]=minVal(minValue[t],g[i][t]);
				}
			}
			else if(d[i]<d[t]+g[i][t] && g[i][t]!=0)
			{
				minValue[i]=maxVal(minValue[i],minVal(minValue[t],g[i][t]));
			}
			
		}
	} 
}

int main()
{
    char str1[21];
    char str2[21];
    int ton;
    int i;
	int ts=0;
    while(true)
    {
        scanf("%d%d",&n,&m);
        if(!n && !m)
            break;
		num=0;
		ts++;
		memset(g,0,sizeof(0));
        for(i=0;i<m;i++)
        {
            //建图
            cin>>str1>>str2>>ton;
            int id1=getTrieId(str1);
            int id2=getTrieId(str2);
            g[id1][id2]=ton;
            g[id2][id1]=ton;
        }
		cin>>str1>>str2;
		int id1=getTrieId(str1);
        int id2=getTrieId(str2);
		dijkstra(id1,id2);
		printf("Scenario #%d\n",ts);
		printf("%d tons\n\n",minValue[id2]); 

    }
    return 0;
}


 


http://www.niftyadmin.cn/n/862553.html

相关文章

2020.11.9【WGS/GWAS】丨全基因组分析(关联分析)全流程(下)

2021.07.30 已更新数据分析部分内容&#xff1a;点我查看 目录一. 摘要二. SNP 检测与注释三. 群体SNP 过滤四. 群体分层分析4.1 群体进化树分析4.2 群体主成分分析4.3 群体遗传结构分析五. 连锁不平衡分析六. 选择消除分析6.1 Fst 分析6.2 θπ分析七. 全基因组关联分析7.1 性…

在linux中安装 Vmwar Tools

安装Vmwar Tools 选择VM-->install VMware Tools [rootlocalhost ~]# mkdir /mnt/cdrom [rootlocalhost ~]#mount /dev/cdrom /mnt/cdrom/ [rootlocalhost ~]# cd /mnt/cdrom/ [rootlocalhost cdrom]# ls 后有 VMwareTools-6.5.0-118166.i386.rpm VMwareTools-6.5.0-…

Linux 操作系统:deb安装包的安装方法

deb 是debian linus 的安装格式&#xff0c;跟red hat 的rpm相似安装&#xff1a; dpkg -i file.deb 不过要安装dpkg的package,也可用alien这类软件将package转为rpm等格式&#xff0c;或直接下个rpm 或tar包。 关于deb包转换成rpm的方法&#xff1a; sudo apt-get install a…

Linux(CentOS)下的apache服务器配置与管理

一、WEB服务器与Apache1、web服务器与网址2、Apache的历史3、补充http://www.netcraft.com/可以查看apache服务器的市场占有率同时必须注意的是ngnix&#xff0c;正处于强势增长的上升时期&#xff0c;大有和apache一争天下的感觉&#xff0c;真是后生可畏~~~二、Apache服务器的…

深入浅析软件需求的定义

软件需求的定义 软件行业存在这样一个问题&#xff0c;用于描述需求工作的术语没有统一的定义。对同一项需求&#xff0c;不同的人会有不同的描述&#xff0c;称其为用户需求、软件需求、功能需求、系统需求、技术需求、业务需求或产品需求。客户对需求的定义&#xff0c;在开发…

IBM公司发展史

IBM公司 机器公司(International Business Machines Corporation ,IBM)是一家拥有40万中层干部,520亿美元资产的大型企业,其年销售额达到500多亿美元,利润为70多亿美元。它是世界上经营最好、管理最成功的公司之一。在计算机——这个发展最迅速、经营最活跃的行业里,其销量居世…

C++实现Chain Of Responsibility模式

由于CSDN不能正常显示图片&#xff0c;本文暂时发表在&#xff1a; http://patmusing.blog.163.com/blog/static/13583496020101501114178/

惠普发展史

惠普发展史 惠普公司 Hewlett-Packard是世界最大的计算机公司之一。该公司制造的产品正被个人使用或用于工业、商业、工程、科学和教育等领域。 该公司在其1998财务年度营业纯收入为424亿美元。 HP总部设在加利福尼亚州的Palo Alto,该公司有雇员8万多人。HP公司在美国许多城市…