• HDU 5441 Travel


    Travel

    Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

    Description

    Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are $n$ cities and $m$ bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another and that the time Jack can stand staying on a bus is $x$ minutes, how many pairs of city $(a, b)$ are there that Jack can travel from city $a$ to $b$ without going berserk?
     

    Input

     
    The first line contains one integer $T, T leq 5$, which represents the number of test case.

    For each test case, the first line consists of three integers $n, m$ and $q$ where $n leq 20000, m leq 100000, q leq 5000$. The Undirected Kingdom has $n$ cities and $m$ bidirectional roads, and there are $q$ queries.

    Each of the following $m$ lines consists of three integers $a, b$ and $d$ where $a, b ∈ {1, . . . , n}$ and $d leq 100000$. It takes Jack $d$ minutes to travel from city $a$ to city $b$ and vice versa.

    Then $q$ lines follow. Each of them is a query consisting of an integer $x$ where $x$ is the time limit before Jack goes berserk.

     

    Output

    You should print $q$ lines for each test case. Each of them contains one integer as the number of pair of cities $(a, b)$ which Jack may travel from $a$ to $b$ within the time limit $x$.

    Note that $(a, b)$ and $(b, a)$ are counted as different pairs and $a$ and $b$ must be different cities.
     

    Sample Input

    1
    5 5 3
    2 3 6334
    1 5 15724
    3 5 5705
    4 3 12382
    1 3 21726
    6000
    10000
    13000
     

    Sample Output

    2
    6
    12
     
    解题:带权并查集 + 离线处理
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 20010;
     4 using LL = long long;
     5 int uf[maxn];
     6 LL ret,ans[maxn],cnt[maxn];
     7 struct arc {
     8     int u,v,w;
     9     bool operator<(const arc &rhs) const {
    10         return w < rhs.w;
    11     }
    12 } e[500010];
    13 struct QU {
    14     int w,id;
    15     bool operator<(const QU &rhs) const{
    16         return w < rhs.w;
    17     }
    18 } Q[maxn];
    19 int Find(int x) {
    20     if(x != uf[x]) uf[x] = Find(uf[x]);
    21     return uf[x];
    22 }
    23 bool Union(int x,int y) {
    24     x = Find(x);
    25     y = Find(y);
    26     if(x == y) return false;
    27     ret -= cnt[x]*(cnt[x] - 1) + cnt[y]*(cnt[y] - 1);
    28     ret += (cnt[x] + cnt[y])*(cnt[x] + cnt[y] - 1);
    29     if(cnt[x] < cnt[y]) {
    30         uf[x] = y;
    31         cnt[y] += cnt[x];
    32         cnt[x] = 0;
    33     } else {
    34         uf[y] = x;
    35         cnt[x] += cnt[y];
    36         cnt[y] = 0;
    37     }
    38     return true;
    39 }
    40 int main() {
    41     int kase,n,m,q;
    42     scanf("%d",&kase);
    43     while(kase--) {
    44         scanf("%d%d%d",&n,&m,&q);
    45         for(int i = 0; i < m; ++i)
    46             scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
    47         for(int i = 0; i < q; ++i) {
    48             scanf("%d",&Q[i].w);
    49             Q[i].id = i;
    50         }
    51         sort(e,e + m);
    52         sort(Q,Q + q);
    53         for(int i = 0; i <= n; ++i) {
    54             uf[i] = i;
    55             cnt[i] = 1;
    56         }
    57         ret = 0;
    58         for(int i = 0,j = 0; i < q; ++i) {
    59             while(j < m && e[j].w <= Q[i].w) {
    60                 Union(e[j].u,e[j].v);
    61                 ++j;
    62             }
    63             ans[Q[i].id] = ret;
    64         }
    65         for(int i = 0; i < q; ++i)
    66             printf("%I64d
    ",ans[i]);
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    swift NSComparator
    Java mac 上编写Java代码
    四舍五入、上取整、下取整
    数组排序
    删除xcode 里的多余证书
    启动画面 设置
    CGFloat Float 互转
    navigationController pop的几种方法
    iOS 获取键盘相关信息
    eclipse代码格式化
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4964003.html
Copyright © 2020-2023  润新知