Description
Nowadays, people have many ways to save money on accommodation when they are on vacation.
One of these ways is exchanging houses with other people.
Here is a group of N people who want to travel around the world. They live in different cities, so they can travel to some other people's city and use someone's house temporary. Now they want to make a plan that choose a destination for each person. There are 2 rules should be satisfied:
1. All the people should go to one of the other people's city.
2. Two of them never go to the same city, because they are not willing to share a house.
They want to maximize the sum of all people's travel distance. The travel distance of a person is the distance between the city he lives in and the city he travels to. These N cities have N - 1 highways connecting them. The travelers always choose the shortest path when traveling.
Given the highways' information, it is your job to find the best plan, that maximum the total travel distance of all people.
Input
The first line of input contains one integer T(1 <= T <= 10), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(2 <= N <= 105), representing the number of cities.
Then the followingN-1 lines each contains three integersX, Y,Z(1 <= X, Y <= N, 1 <= Z <= 106), means that there is a highway between city X and city Y , and length of that highway.
You can assume all the cities are connected and the highways are bi-directional.
Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y represents the largest total travel distance of all people.
Sample Input
2
4
1 2 3
2 3 2
4 3 2
6
1 2 3
2 3 4
2 4 1
4 5 8
5 6 5
Sample Output
Case #1: 18
暂时先写这么多,跪求大神。。。
Nowadays, people have many ways to save money on accommodation when they are on vacation.
One of these ways is exchanging houses with other people.
Here is a group of N people who want to travel around the world. They live in different cities, so they can travel to some other people's city and use someone's house temporary. Now they want to make a plan that choose a destination for each person. There are 2 rules should be satisfied:
1. All the people should go to one of the other people's city.
2. Two of them never go to the same city, because they are not willing to share a house.
They want to maximize the sum of all people's travel distance. The travel distance of a person is the distance between the city he lives in and the city he travels to. These N cities have N - 1 highways connecting them. The travelers always choose the shortest path when traveling.
Given the highways' information, it is your job to find the best plan, that maximum the total travel distance of all people.
Input
The first line of input contains one integer T(1 <= T <= 10), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(2 <= N <= 105), representing the number of cities.
Then the followingN-1 lines each contains three integersX, Y,Z(1 <= X, Y <= N, 1 <= Z <= 106), means that there is a highway between city X and city Y , and length of that highway.
You can assume all the cities are connected and the highways are bi-directional.
Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y represents the largest total travel distance of all people.
Sample Input
2
4
1 2 3
2 3 2
4 3 2
6
1 2 3
2 3 4
2 4 1
4 5 8
5 6 5
Sample Output
Case #1: 18
Case #2: 62
又是做了一道超时的。。。
思路是每次找一条树枝,然后就 bfs 出最长路径,那肯定也是一条树枝,然后就删掉这两条树枝,继续第二轮循环,直至全部路线被删除。
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <queue>
#include <cmath>
using namespace std;
// 用来进行 bfs 的结构
struct Item
{
unsigned int len;
int num;
};
// 邻接表结构。。。
struct Road
{
unsigned int len;
int to;
Road *next;
};
Road *roads[100005];
bool visit[100005];
int t,n;
int deal()
{
// 每次从一个树枝开始搜索最远的路径
// 搜索完成后,加入最后结果,并将这两条
// 树枝从邻接表中删除
unsigned int res = 0;
unsigned int max_len = 0;
int goal;
while (true)
{
bool finish = true;
for (int i=0;i<n;i++)
visit[i] = false;
for (int i=0;i<n;i++)
{
// 只有一个元素就是一条树枝
if (roads[i]!=NULL && roads[i]->next==NULL)
{
finish = false;
// 以 i 为起点进行 bfs
queue<Item> q;
Item tmp = {0, i};
q.push(tmp);
while (!q.empty())
{
Item cur = q.front();
q.pop();
visit[cur.num] = true;
// 遍历邻接表并将路径与最大路径进行比较
Road *r = roads[cur.num];
while (r != NULL)
{
if (!visit[r->to])
{
Item tmp;
tmp.len = cur.len + r->len;
tmp.num = r->to;
if (tmp.len > max_len)
{
max_len = tmp.len;
goal = tmp.num;
}
q.push(tmp);
}
r = r->next;
}
}
// bfs 后,最大路径长,删除两个树枝
res += 2*max_len;
//cout << max_len << endl;
max_len = 0;
delete roads[i];
delete roads[goal];
roads[i] = roads[goal] = NULL;
// 删除其他结点中的树枝
for (int j=0;j<n;j++)
{
Road *f = roads[j];
if (f == NULL) continue;
if (f->to==i || f->to==goal)
{
roads[j] = f->next;
delete f;
continue;
}
Road *r = f->next;
while (r != NULL)
{
if (r->to==i || r->to==goal)
{
f->next = r->next;
delete r;
break;
}
f = r;
r = r->next;
}
}
break;
} // 结束一次搜索过程
}
if (finish)
break;
}
return res;
}
int main()
{
cin >> t;
int index = 1;
while (t--)
{
cin >> n;
// initializing
for (int i=0;i<n;i++)
roads[i] = NULL;
int p, q, l;
for (int i=0;i<n-1;i++)
{
scanf("%d %d %d", &p, &q, &l);
// 构造邻接表
Road *tmp = new Road;
tmp->len = l;
tmp->to = q-1;
tmp->next = roads[p-1];
roads[p-1] = tmp;
tmp = new Road;
tmp->len = l;
tmp->to = p-1;
tmp->next = roads[q-1];
roads[q-1] = tmp;
}
printf("Case #%d: %d\n", index++, deal());
// 删除邻接表...
}
system("pause");
return 0;
}
暂时先写这么多,跪求大神。。。