zoukankan      html  css  js  c++  java
  • hdu 5441

    Travel

    Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 4685    Accepted Submission(s): 1535


    Problem 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,T5, which represents the number of test case.

    For each test case, the first line consists of three integers n,m and q where n20000,m100000,q5000. The Undirected Kingdom has n cities and mbidirectional 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 d100000. 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
     
    Source
     
    Recommend
    hujie   |   We have carefully selected several similar problems for you:  6361 6360 6359 6358 6357 
     
     
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<iostream>
     6 #include<vector>
     7 using namespace std;
     8 typedef long long ll;
     9 #define N 100009
    10 #define ls l,m,rt<<1
    11 #define rs m+1,r,rt<<1|1
    12 #define mem(a,b) memset(a,b,sizeof(a))
    13 int  t,n,m,q;
    14 struct Node{
    15     int u,v,w;
    16 }nod[N];
    17 struct Nod{
    18     int id,w;
    19 }no[N];
    20 int pre[20009];
    21 int cnt[20009];
    22 bool cmp(Node a,Node b){
    23     return a.w<b.w;
    24 }
    25 bool cmp1(Nod a,Nod b){
    26     return a.w<b.w;
    27 }
    28 void init()
    29 {
    30     for(int i=0;i<=n;i++){
    31         pre[i]=i;
    32         cnt[i]=1;
    33     }
    34 }
    35 int find(int x)
    36 {
    37     return pre[x]=x==pre[x]?x:find(pre[x]);
    38 }
    39 void unite(int u,int v)
    40 {  /*
    41     pre[v]=u;
    42     cnt[u]+=cnt[v];
    43     */
    44     pre[u]=v;
    45     cnt[v]+=cnt[u];
    46     //上面两种写法都是对的。
    47 }
    48 int ret[N];
    49 int  main()
    50 {
    51     scanf("%d",&t);
    52     while(t--)
    53     {
    54         scanf("%d%d%d",&n,&m,&q);
    55         init();
    56         for(int i=0;i<m;i++){
    57             scanf("%d%d%d",&nod[i].u,&nod[i].v,&nod[i].w);
    58             }
    59             sort(nod,nod+m,cmp);
    60             for(int i=0;i<q;i++){
    61                 scanf("%d",&no[i].w);
    62                 no[i].id=i;
    63             }
    64             sort(no,no+q,cmp1);
    65             int j=0,ans=0;
    66             int u,v;
    67             //预处理
    68             for(int i=0;i<q;i++){
    69                 while(j<m&&nod[j].w<=no[i].w){
    70                     u=find(nod[j].u);
    71                     v=find(nod[j].v);
    72                     if(u!=v){
    73                         int tmp=cnt[v]+cnt[u];
    74                         //任意一对点都行
    75                         ans+=(tmp*(tmp-1))-(cnt[u]*(cnt[u]-1))-(cnt[v]*(cnt[v]-1));
    76                         //减去已经加过的
    77                         unite(u,v);
    78                     }
    79                     j++;
    80                 }
    81                 ret[no[i].id]=ans;
    82             }            
    83             for(int i=0;i<q;i++){
    84                 printf("%d
    ",ret[i]);
    85             }        
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    poj3041(最小顶点覆盖)
    High-speed Charting Control--MFC绘制图表(折线图、饼图、柱形图)控件
    hdu 3183 A Magic Lamp(RMQ)
    Android studio 中创建AIDL Service
    cocos2d-x 3.0正式版 cmd创建project以及一键创建project
    【Machine Learning】决策树案例:基于python的商品购买能力预测系统
    【Machine Learning】机器学习及其基础概念简介
    【Machine Learning】Python开发工具:Anaconda+Sublime
    【HanLP】HanLP中文自然语言处理工具实例演练
    【HanLP】资料链接汇总
  • 原文地址:https://www.cnblogs.com/tingtin/p/9441751.html
Copyright © 2011-2022 走看看