• 图论trainning-part-1 H. Qin Shi Huang's National Road System


    H. Qin Shi Huang's National Road System

    Time Limit: 1000ms
    Memory Limit: 32768KB
    64-bit integer IO format: %I64d      Java class name: Main
     
    During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified China in 221 BC. That was Qin dynasty ---- the first imperial dynasty of China(not to be confused with the Qing Dynasty, the last dynasty of China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor" in Chinese.

    Qin Shi Huang undertook gigantic projects, including the first version of the Great Wall of China, the now famous city-sized mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a story about the road system:
    There were n cities in China and Qin Shi Huang wanted them all be connected by n-1 roads, in order that he could go to every city from the capital city Xianyang.
    Although Qin Shi Huang was a tyrant, he wanted the total length of all roads to be minimum,so that the road system may not cost too many people's life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could build a road by magic and that magic road would cost no money and no labor. But Xu Fu could only build ONE magic road for Qin Shi Huang. So Qin Shi Huang had to decide where to build the magic road. Qin Shi Huang wanted the total length of all none magic roads to be as small as possible, but Xu Fu wanted the magic road to benefit as many people as possible ---- So Qin Shi Huang decided that the value of A/B (the ratio of A to B) must be the maximum, which A is the total population of the two cites connected by the magic road, and B is the total length of none magic roads.
    Would you help Qin Shi Huang?
    A city can be considered as a point, and a road can be considered as a line segment connecting two points.
     

    Input

    The first line contains an integer t meaning that there are t test cases(t <= 10).
    For each test case:
    The first line is an integer n meaning that there are n cities(2 < n <= 1000).
    Then n lines follow. Each line contains three integers X, Y and P ( 0 <= X, Y <= 1000, 0 < P < 100000). (X, Y) is the coordinate of a city and P is the population of that city.
    It is guaranteed that each city has a distinct location.
     

    Output

    For each test case, print a line indicating the above mentioned maximum ratio A/B. The result should be rounded to 2 digits after decimal point.
     

    Sample Input

    2
    4
    1 1 20
    1 2 30
    200 2 80
    200 1 100
    3
    1 1 20
    1 2 30
    2 2 40

    Sample Output

    65.00
    70.00

    解题:此题跟次小生成树没多大关系,只是涉及到最小生成树的遍历问题。解题思路就是先求出最小生成树,存储这棵树,然后再在这棵树上进行去边操作,此时的两颗树上人口数最多的两个城市人口数的和 去除以 最小生成树的值减去此边的值 后的商,求这个商最大可能是多少。

    Kruskal写法
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 #define INF 0x3f3f3f
    11 using namespace std;
    12 const int maxv = 1010;
    13 const int maxe = 500*1010;
    14 struct arc{
    15     int u,v;
    16     double w;
    17 };
    18 int uf[maxv];
    19 vector<int>g[maxv];
    20 arc e[maxe],tree[maxe];
    21 int n,px[maxv],py[maxv],val[maxv];
    22 int maxVal,es,ads;
    23 double Minst;
    24 bool vis[maxv];
    25 bool cmp(const arc &x,const arc &y){
    26     return x.w < y.w;
    27 }
    28 void dfs(int u){
    29     vis[u] = true;
    30     if(val[u] > maxVal) maxVal = val[u];
    31     for(int i = 0; i < g[u].size(); i++){
    32         if(!vis[g[u][i]]) dfs(g[u][i]);
    33     }
    34 }
    35 int findF(int x){
    36     if(x != uf[x])
    37         uf[x] = findF(uf[x]);
    38     return uf[x];
    39 }
    40 double dis(int i,int j){
    41     double temp = (px[i]-px[j])*(px[i]-px[j])+(py[i]-py[j])*(py[i]-py[j]);
    42     return sqrt(temp);
    43 }
    44 void kruskal(){
    45     for(int i = 0; i < es; i++){
    46         int x = findF(e[i].u);
    47         int y = findF(e[i].v);
    48         if(x != y){
    49             Minst += e[i].w;
    50             uf[x] = y;
    51             g[e[i].u].push_back(e[i].v);
    52             g[e[i].v].push_back(e[i].u);
    53             tree[ads++] = e[i];
    54         }
    55     }
    56 }
    57 int main(){
    58     int t,i,j;
    59     scanf("%d",&t);
    60     while(t--){
    61         scanf("%d",&n);
    62         for(i = 1; i <= n; i++){
    63             scanf("%d%d%d",px+i,py+i,val+i);
    64             g[i].clear();
    65             uf[i] = i;
    66         }
    67         ads = es = 0;
    68         Minst = 0;
    69         for(i = 1; i <= n; i++)
    70             for(j = i+1; j <= n; j++){
    71                 e[es++] = (arc){i,j,dis(i,j)};
    72             }
    73         sort(e,e+es,cmp);
    74         kruskal();
    75         double ans = 0,temp;
    76         for(i = 0; i < ads; i++){
    77             int u = tree[i].u;
    78             int v = tree[i].v;
    79             memset(vis,false,sizeof(vis));
    80             vis[v] = true;
    81             temp = maxVal = 0;
    82             dfs(u);
    83             temp += maxVal;
    84             memset(vis,false,sizeof(vis));
    85             maxVal = 0;
    86             vis[u] = true;
    87             dfs(v);
    88             temp += maxVal;
    89             ans = max(ans,temp/(Minst-tree[i].w));
    90         }
    91         printf("%.2f
    ",ans);
    92     }
    93     return 0;
    94 }
    View Code

    Prim写法:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #define LL long long
    13 #define INF 0x3f3f3f3f
    14 using namespace std;
    15 const int maxv = 1010;
    16 const int maxe = 500*1010;
    17 struct arc{
    18     int u,v;
    19     double w;
    20 }e[maxe];
    21 int px[maxv],py[maxv],val[maxv],pre[maxv];
    22 double d[maxv],Minst,mp[maxv][maxv];
    23 int n,tot,mxp;
    24 vector<int>g[maxv];
    25 bool vis[maxv];
    26 double dis(int i,int j){
    27     double temp = (px[i]-px[j])*(px[i]-px[j])+(py[i]-py[j])*(py[i]-py[j]);
    28     return sqrt(temp);
    29 }
    30 void prim(){
    31     int i,j,index;
    32     double theMin;
    33     for(i = 2; i <= n; i++){
    34         d[i] = mp[1][i];
    35         pre[i] = 1;
    36     }
    37     for(i = 1; i < n; i++){
    38         theMin = INF;
    39         for(j = 2; j <= n; j++){
    40             if(d[j] > 0 && d[j] < theMin) theMin = d[index = j];
    41         }
    42         e[tot++] = (arc){pre[index],index,theMin};
    43         g[index].push_back(pre[index]);
    44         g[pre[index]].push_back(index);
    45         Minst += theMin;
    46         d[index] = -1;
    47         for(j = 2; j <= n; j++)
    48         if(d[j] > 0 && d[j] > mp[index][j]){
    49             d[j] = mp[index][j];
    50             pre[j] = index;
    51         }
    52     }
    53 }
    54 void dfs(int u){
    55     vis[u] = true;
    56     if(val[u] > mxp) mxp = val[u];
    57     for(int i = 0; i < g[u].size(); i++){
    58         if(!vis[g[u][i]]) dfs(g[u][i]);
    59     }
    60 }
    61 int main(){
    62     int t,i,j;
    63     scanf("%d",&t);
    64     while(t--){
    65         scanf("%d",&n);
    66         for(i = 1; i <= n; i++){
    67             scanf("%d%d%d",px+i,py+i,val+i);
    68             g[i].clear();
    69         }
    70         for(i  = 1; i <= n; i++){
    71             for(j = i+1; j <= n; j++)
    72                 mp[i][j] = mp[j][i] = dis(i,j);
    73         }
    74         Minst = 0;
    75         tot = 0;
    76         prim();
    77         double ans = 0,temp;
    78         for(i = 0; i < tot; i++){
    79             int u = e[i].u;
    80             int v = e[i].v;
    81             memset(vis,false,sizeof(vis));
    82             temp = 0;
    83             mxp = 0;
    84             vis[v] = true;
    85             dfs(u);
    86             temp += mxp;
    87             memset(vis,false,sizeof(vis));
    88             vis[u] = true;
    89             mxp = 0;
    90             dfs(v);
    91             temp += mxp;
    92             ans = max(ans,temp/(Minst-e[i].w));
    93         }
    94         printf("%.2f
    ",ans);
    95     }
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    [MySQL优化案例]系列 — 分页优化
    [MySQL优化案例]系列 — RAND()优化
    CSS模块化思想-----命名是个技术活
    php curl选项列表(超详细)
    CURL使用介绍
    HTTP头信息
    git常用命令
    Git .gitignore文件说明
    yield(),wait(),sleep(),join()
    Java对象序列化和返序列化
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3862771.html
Copyright © 2020-2023  润新知