Gold Transportation (二分答案 + dfs)

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<d<=10000), means that there is a road between x and y for distance of d. N=0 means end of the input.

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

题意:有n个城市,每个城市地下有v[i]的矿,并且每个城市有w[i]的容器,有m条道路,每条道路长为e[i].len,当采完所有城市的矿后,可能该城市的容器不够大,需要运输到其他城市去储存,问在运输过程中,不同运输方式的所有被使用路线中的最大长度最小是多少?

题解:将所有边按照长度排序,二分答案,dfs遍历图判断是否满足条件

AC代码:

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node<<1
#define rson mid + 1, r, node<<1|1
#define Root 1, m, 1
#define Node l, r, node
const int INF  =  0x3f3f3f3f;
const int O    =  1e6;
const int mod  =  20071027;
const int maxn =  2e2 + 5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

int n, m;
int v[maxn], w[maxn];
struct dd { int u, v, len;
    bool friend operator < (dd a, dd b) { return a.len < b.len; }
} e[maxn * maxn];

struct Line { int to, len, next;} line[maxn * maxn * 2];
int head[maxn], cnt;
void add(int u, int v, int w) {
    line[cnt] = { v, w, head[u] }; head[u] = cnt ++;
    line[cnt] = { u, w, head[v] }; head[v] = cnt ++;
}

int now, need, vis[maxn];

void dfs(int u) {
    now += w[u]; need += v[u]; vis[u] = 1;
    for(int i=head[u]; i!=-1; i=line[i].next) {
        int to = line[i].to;
        if(vis[to]) continue;
        dfs(to);
    }
}

bool check(int k) {
    MT(head, -1); cnt = 0;
    for(int i=0; i<=k; i++) add(e[i].u, e[i].v, e[i].len);
    MT(vis, 0);
    for(int i=1; i<=n; i++) {
        if(vis[i]) continue;
        now = 0; need = 0;  dfs(i);
        if(need > now) return false;
    }
    return true;
}

int main(){
    while(scanf("%d", &n) && n) {
        for(int i=1; i<=n; i++) scanf("%d", &v[i]);
        for(int i=1; i<=n; i++) scanf("%d", &w[i]);
        scanf("%d", &m);
        for(int i=1; i<=m; i++) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].len);
        e[0] = {0, 0, 0}; sort(e, e + m + 1); //加一条0长度的边,因为答案可能为0
        int l = 0, r = m, ans = -1;
        while(l <= r ) {
            int mid = (l + r) >> 1;
            if(check(mid)) { ans = mid ; r = mid - 1; }
            else l = mid + 1;
        }
        if(ans == -1) printf("No Solution\n");
        else printf("%d\n", e[ans].len);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值