Gold Transportation

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 3079 Accepted: 1101

Description

Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the storehouses as quickly as possible. Suppose that the Zorroming State consists of N towns and there are M bidirectional roads among these towns. The gold mines are only discovered in parts of the towns, while the storehouses are also owned by parts of the towns. The storage of the gold mine and storehouse for each town is finite. The truck drivers in the Zorroming State are famous for their bad temper that they would not like to drive all the time and they need a bar and an inn available in the trip for a good rest. Therefore, your task is to minimize the maximum adjacent distance among all the possible transport routes on the condition that all the gold is safely transported to the storehouses.

Input

The input contains several test cases. For each case, the first line is integer N(1<=N<=200). The second line is N integers associated with the storage of the gold mine in every towns .The third line is also N integers associated with the storage of the storehouses in every towns .Next is integer M(0<=M<=(n-1)*n/2).Then M lines follow. Each line is three integers x y and d(1<=x,y<=N,0

Output

For each case, output the minimum of the maximum adjacent distance on the condition that all the gold has been transported to the storehouses or “No Solution”.

Sample Input

4

3 2 0 0

0 0 3 3

6

1 2 4

1 3 10

1 4 12

2 3 6

2 4 8

3 4 5

0

Sample Output

6


解题心得:

  1. 题意就是很多个点,每个点有等待放置的金子也有可以储存的金子的量,给你一些边,你选择这些边连接的点的金子可以相互转移,最后需要让所有的金子都能够被储存,并且要求选择出来的边要最大的边最小。
  2. 这个就是一个最小生成树,记录一个点有的金子为正值,可以储存的值为负值,然后选择边将点连接起来,连接起来的点要将值相加,最后只要所有连接起来的点没有大于0的值就可以停止加边了。
  3. 关于最大的边最小的问题,可以直接使用Kruskal算法,在Kruskal的算法之中,添加的边都是从小开始添加的,所以使用Kruskal算法,得到的最小生成树最大的边就是最大边中的最小。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000;
int res[maxn],father[maxn],Sum,n,m;
struct NODE
{
int s,e,len;
} node[maxn*maxn]; bool cmp(NODE a,NODE b)
{
return a.len < b.len;
} void init()
{
memset(res,0,sizeof(res));
memset(node,0,sizeof(node));
for(int i=0;i<maxn;i++)
father[i] = i;
//有金子的量为正,可以储存的量为负
for(int i=1; i<=n; i++)
scanf("%d",&res[i]);
for(int i=1; i<=n; i++)
{
int x;
scanf("%d",&x);
res[i] -= x;
}
scanf("%d",&m);
for(int i=0; i<m; i++)
scanf("%d%d%d",&node[i].s,&node[i].e,&node[i].len);
sort(node,node+m,cmp);
} int find(int x)
{
if(x == father[x])
return x;
return father[x] = find(father[x]);
} bool check()//检查是不是所有的点都是不大于0的
{
for(int i=1;i<=n;i++)
{
int fx = find(i);
if(res[fx] > 0)
return false;
}
return true;
} void solve()
{
int Max_path = 0;
for(int i=0; i<m; i++)
{
if(check())//如果出现的点全是小于等于0的直接跳出
break;
int s = node[i].s;
int e = node[i].e;
int fx = find(s);
int fy = find(e);
if(fx == fy)
continue;
int len = node[i].len;
if(len > Max_path)
Max_path = len;
int Min = min(res[fx],res[fy]);
int Max = max(res[fx],res[fy]);
father[fx] = fy;
res[fy] = Min+Max;//合并之后将值也合并
}
if(!check())
printf("No Solution\n");
else
printf("%d\n",Max_path);
} int main()
{
while(cin>>n && n)
{
init();
solve();
}
return 0;
}

POJ:3228-Gold Transportation(要求最小生成树最大边最小)的更多相关文章

  1. POJ 3228 Gold Transportation

    Gold Transportation Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Ori ...

  2. POJ 3228 Gold Transportation(带权并查集,好题)

    参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...

  3. poj 3228 Gold Transportation 二分+网络流

    题目链接 给出n个城市, 每个城市有一个仓库, 仓库有容量限制, 同时每个城市也有一些货物, 货物必须放到仓库中. 城市之间有路相连, 每条路有长度. 因为有些城市的货物量大于仓库的容量, 所以要运到 ...

  4. POJ 1797 Heavy Transportation 【最大生成树的最小边/最小瓶颈树】

    Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...

  5. poj 1797 Heavy Transportation(最大生成树)

    poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...

  6. POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...

  7. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  8. POJ 3241 Object Clustering 曼哈顿最小生成树

    Object Clustering   Description We have N (N ≤ 10000) objects, and wish to classify them into severa ...

  9. 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

    [POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit ...

随机推荐

  1. 什么是语义化的HTML?有何意义?为什么要做到语义化?

    一.什么是语义化的HTML? 语义化的HTML就是正确的标签做正确的事情,能够便于开发者阅读和写出更优雅的代码的同时让网络爬虫很好地解析. 二.为什么要做到语义化? 1.有利于SEO,有利于搜索引擎爬 ...

  2. STM32之ADC(内部基准电压,参考电压)

    转 STM32内部参照电压VREFIN的使用 https://blog.csdn.net/uncle_guo/article/details/50625660 每个STM32芯片都有一个内部的参照电压 ...

  3. Azkaban2.5安装部署(系统时区设置 + 安装和配置mysql + Azkaban Web Server 安装 + Azkaban Executor Server安装 + Azkaban web server插件安装 + Azkaban Executor Server 插件安装)(博主推荐)(五)

    Azkaban是什么?(一) Azkaban的功能特点(二) Azkaban的架构(三) Hadoop工作流引擎之Azkaban与Oozie对比(四) 不多说,直接上干货! http://www.cn ...

  4. Spark Mllib里决策树回归分析如何对numClasses无控制和将部分参数设置为variance(图文详解)

    不多说,直接上干货! 在决策树二元或决策树多元分类参数设置中: 使用DecisionTree.trainClassifier   见 Spark Mllib里如何对决策树二元分类和决策树多元分类的分类 ...

  5. Ionic开发-常用插件安装

      cordova plugin add cordova-plugin-nativestoragecordova plugin add cordova-plugin-devicecordova plu ...

  6. Object类-try-catch-finally-throw-throws-自定义异常

    一.Object类     Object类是最顶端的类,其它类都是它的子类,它的方法可以被继承.如果定义的类没有继承别的类,那它的直接父类就是Object.如果方法参数类型为object类型, 则调用 ...

  7. uvm_reg_map——寄存器模型(八)

    所有的寄存器都需要地址,都需要加入到地址列表中 //-------------------------------------------------------------------------- ...

  8. shell命令cut

    cut命令用来操作字符串,可以理解为剪切字符串的工具: cut有两种用法: 1.剪切字符串中的单个字符(-c参数) 例如: str=abcdef echo $str | cut -c 1-1 输出:a ...

  9. 洛谷 P1433 吃奶酪

    题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...

  10. 撤销git pull命令

    比如:在master分支上执行了git pull命令,想回到pull之前分支所在的commit位置. 步骤一:用 git reflog master 查看master分支的历史变动记录,其中有一个就是 ...