HDU 4081 Qin Shi Huang's National Road System【次小生成树】【模板题】
Qin Shi Huang's National Road System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8054 Accepted Submission(s): 2869
Problem Description
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.
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.
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
Source
题意:在生成树中找到一条边,使得这条边上两点的费用之和除以生成树中剩下的n-2条边的边权之和最大。
Kruskal做法:
思路参考:点击打开链接
本题数据有稠密图,Kruskal的复杂度会达到O(n^3)导致在c++下超时,G++可AC
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<iomanip>
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 1010;
int x[maxn], y[maxn];
int peo[maxn]; //记录每个城市人数
int par[maxn]; //记录前驱
int n; //点的数量
double maxDist[maxn][maxn]; //从i->j的在最小生成树中的最大的边
double getDist(int p, int q) //求两点间距离
{
return sqrt((x[p] - x[q])*(x[p] - x[q]) + (y[p] - y[q])*(y[p] - y[q]));
}
int num; //记录图上边的数量
int cnt; //最小生成树边下标记录
int index[maxn * 2]; //最小生成树前一节点记录以连接成树
struct Edge { //记录原图的所有边
int u, //起点
v; //终点
double d; //距离
bool flag; //是否加入最小生成树
}edge[maxn*maxn];
void addEdge(int st, int ed) //添加图上的边
{
edge[num].u = st;
edge[num].v = ed;
edge[num].d = getDist(st, ed);
edge[num++].flag = 0;
}
struct Tree { //记录最小生成树
int to;
double w;
int next;
}tree[maxn * 2];
void addtree(int st, int ed,double w) //添加最小生成树的边
{
tree[cnt].to = ed;
tree[cnt].w = w;
tree[cnt].next = index[st];
index[st] = cnt++;
}
void init()
{
ms(par, -1);
ms(index, -1);
}
int find(int x)
{
return par[x] == -1 ? x : par[x] = find(par[x]);
}
bool isunite(int x, int y)
{
return find(x) == find(y);
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
par[x] = y;
}
bool cmp(Edge a, Edge b)
{
return a.d < b.d;
}
double kruskal()
{
init();
double mst = 0;
sort(edge, edge + num, cmp);
int edgenum = 0; //最小生成树边数
for (int i = 0; i < num; i++) //枚举边数
{
if (!isunite(edge[i].u, edge[i].v))
{
unite(edge[i].u, edge[i].v);
edgenum++;
addtree(edge[i].u, edge[i].v, edge[i].d);
addtree(edge[i].v, edge[i].u, edge[i].d);
edge[i].flag = 1; //标记为已使用
mst += edge[i].d;
}
if (edgenum == n - 1) break;
}
return edgenum == n - 1 ? mst : -1;
}
struct Data { //记录当前所遍历过来的点和最大的边
int id;
double d;
Data(int u, int v)
{
id = u;
d = v;
}
};
void bfs(int p)
{
queue<Data> que;
bool vis[maxn] = { 0 };
vis[p] = 1;
que.push(Data(p, 0));
while (que.size())
{
Data now = que.front(); que.pop();
for (int i = index[now.id]; i != -1; i = tree[i].next)
{
int end = tree[i].to;
double d = tree[i].w;
if (!vis[end])
{
vis[end] = 1;
if (now.d > d) d = now.d;
maxDist[p][end] = d;
que.push(Data(end, d));
}
}
}
}
void secondMst(double mst)
{
for (int i = 0; i < n; i++) bfs(i);
double ans = -1;
for (int i = 0; i < num; i++) //遍历所有边
{
double temp = 0;
int u = edge[i].u, v = edge[i].v;
if (edge[i].flag) //该边在最小生成树选中的边中
{
temp = (peo[u] + peo[v])*1.0 / (mst - edge[i].d); //直接减去这条边
}
else
{
temp = (peo[u] + peo[v])*1.0 / (mst - maxDist[u][v]); //减去最大边
}
ans = max(ans, temp);
}
printf("%.2f\n", ans);
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
cnt = 0;
num = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", x + i, y + i, peo + i);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++) //不能产生重边
{
addEdge(i, j);
}
}
double mst = kruskal();
secondMst(mst);
}
return 0;
}
Prim做法:
可以参考:点击打开链接
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<iomanip>
#define INF 0x3f3f3f3f
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 1010;
int x[maxn], y[maxn];
int peo[maxn]; //记录每个城市人数
int n; //点的数量
double cost[maxn][maxn];
double maxDist[maxn][maxn]; //从i->j的在最小生成树中的最大的边
bool used[maxn][maxn] = { 0 }; //标记边是否使用
double getDist(int p, int q) //求两点间距离
{
return sqrt((x[p] - x[q])*(x[p] - x[q]) + (y[p] - y[q])*(y[p] - y[q]));
}
void prim()
{
double mst = 0; //最小生成树权值
double mincost[maxn] = { 0 }; //最小生成树到其他点的距离
int pre[maxn] = { 0 }; //该结点的前驱
bool vis[maxn] = { 0 }; //标记访问
ms(maxDist, 0);
ms(used, 0);
for (int i = 1; i < n; i++)
{
mincost[i] = cost[0][i];
pre[i] = 0;
}
pre[0] = -1;
mincost[0] = 0;
vis[0] = 1;
while (1)
{
int v = -1;
for (int u = 0; u < n; u++)
{
if (!vis[u] && (v == -1 || mincost[u] < mincost[v])) v = u;
}
if (v == -1) break;
vis[v] = 1;
used[v][pre[v]] = used[pre[v]][v] = 1;
mst += mincost[v];
for (int u = 0; u < n; u++)
{
if (vis[u] && u != v) //只有对已经访问过的点才能使用这个更新条件
{
maxDist[u][v] = maxDist[v][u] = max(maxDist[u][pre[v]], mincost[v]);
}
if (!vis[u] && mincost[u] > cost[v][u]) //一定要加vis,负责访问过的点前继可能被换导致错误
{
mincost[u] = cost[v][u];
pre[u] = v;
}
}
}
double ans = -1;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (used[i][j])
{
ans = max(ans, (peo[i] + peo[j])*1.0 / (mst - cost[i][j]));
}
else
{
ans = max(ans, (peo[i] + peo[j])*1.0 / (mst - maxDist[i][j]));
}
}
}
printf("%.2f\n", ans);
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d%d%d", x + i, y + i, peo + i);
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
cost[i][j] = cost[j][i] = getDist(i, j);
}
}
prim();
}
return 0;
}
Fighting~