UVa 544 - Heavy Cargo

题目:有一个载重无限的卡车运输货物,在城市中每条道路有一个能承受的最大重量,

现在用卡车从一个城市到另一个城市运送货物,问最大的运输重量。

分析:图论,最短路,最小生成树。找一条从起点到终点的路径,使得其中最窄的路段最宽。

从起点开始不断向周围扩散,像dijkstra算法和prime算法一样,只是维护最大值即可。

(如果上述方法不正确,则存在在一个确定点,其后面的确定点可以更新它,则在那个点应该先被找到)

说明:道路是双向的,重复的路径认为是扩建,加和处理。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

/*hash_satrt*/
typedef struct hash_node
{
	int  		id;
	char 		name[31];
	hash_node*	next;
}hash_node;
hash_node  Node[202];
hash_node* List[10007];
int        list_size = 0;

void hash_initial()
{
	list_size = 0;
	memset(List, 0, sizeof(List));
	memset(Node, 0, sizeof(Node));
}

int hash_hash(char *word)
{
	int value = 0;
	for (int i = 0 ; word[i] ; ++ i)
		value = value * word[i] % 10007;
	return value;
}

int hash_find(char *word)
{
	int value = hash_hash(word);
	for (hash_node *p = List[value] ; p ; p = p->next)
		if (!strcmp(p->name, word))
			return p->id;
	Node[list_size].id = list_size;
	strcpy(Node[list_size].name, word);
	Node[list_size].next = List[value];
	List[value] = &Node[list_size ++];
	return list_size-1;
}
/*hash_end*/

int  maps[101][101];
int  path[101],used[101];
char buf1[31],buf2[31];

int deal(int s, int t, int n)
{
	for (int i = 0 ; i < n ; ++ i)
		path[i] = used[i] = 0;

	path[s] = 10001;
	used[s] = 1;
	for (int i = 1 ; i < n ; ++ i) {
		for (int j = 0 ; j < n ; ++ j)
			if (!used[j] && maps[s][j])
				path[j] = max(path[j], min(maps[s][j], path[s]));
		int Max = 0;
		for (int j = 0 ; j < n ; ++ j)
			if (!used[j] && Max < path[j]) {
				s = j;
				Max = path[j];
			}
		used[s] = 1;
	}
	return path[t];
}

int main()
{
	int m,n,p,a,b,Case = 1;
	while (~scanf("%d%d",&n,&m) && n) {
		hash_initial();

		memset(maps, 0, sizeof(maps));
		for (int i = 0 ; i < m ; ++ i) {
			scanf("%s%s%d",buf1,buf2,&p);
			a = hash_find(buf1);
			b = hash_find(buf2);
			maps[a][b] += p;
			maps[b][a] += p;
		}

		scanf("%s%s",buf1,buf2);
		printf("Scenario #%d\n",Case ++);
		printf("%d tons\n\n",deal(hash_find(buf1), hash_find(buf2), n));
	}
    return 0;
}
时间: 2024-10-12 03:48:24

UVa 544 - Heavy Cargo的相关文章

POJ 2263 Heavy Cargo(二分+并查集)

题目地址:POJ 2263 这题是在网上的一篇关于优先队列的博文中看到的..但是实在没看出跟优先队列有什么关系..我用的二分+并查集做出来了... 二分路的载重量.然后用并查集检查是否连通. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #

Poj 2263 Heavy Cargo Floyd 求最大容量路

f[i][j] = max(f[i][j],min(f[i][k],f[j][k])) #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cst

poj2263 Heavy Cargo --- floyd求最大容量路

求给定起点到终点的路径中,最小边权的最大值 #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f

POJ 2263 Heavy Cargo(Floyd + map)

Heavy Cargo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3768   Accepted: 2013 Description Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount

Heavy Cargo POJ 2263 (Floyd传递闭包)

Description Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by

POJ 2263 Heavy Cargo(ZOJ 1952)

最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF,其他为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来,直到出发和终点能沟通的时候,最小的边就是. Kruskal: #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<ma

poj2263 zoj1952 Heavy Cargo(floyd||spfa)

这道题数据范围小,方法比较多.我用floyd和spfa分别写了一下,spfa明显有时间优势. 一个小技巧在于:把城市名称对应到数字序号,处理是用数字. 方法一:spfa #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include&

poj 2263 Heavy Cargo(floyd+dijkstra)

floyd #include<stdio.h> #include<string.h> #include<algorithm> #include<string> #include<map> #include<iostream> using namespace std; int n,m,edge[250][250],vis[250],dist[250]; map<string,int>a; void floyd() { int

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY