HDU4871-Shortest-path tree

Shortest-path tree

                                                                    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 130712/130712 K (Java/Others)
                                                                                            Total Submission(s): 1674    Accepted Submission(s): 566


Problem Description
Given a connected, undirected graph G, a shortest-path tree rooted at vertex v is a spanning tree T of G, such that the path distance from root v to any other vertex u in T is the shortest path distance from v to u in G. 
We may construct a shortest-path tree using the following method:
We consider a shortest-path tree rooted at node 1. For every node i in the graph G, we choose a shortest path from root to i. If there are many shortest paths from root to i, we choose the one that the sequence of passing nodes' number is lexicographically minimum. All edges on the paths that we chose form a shortest-path tree.
Now we want to know how long are the longest simple paths which contain K nodes in the shortest-path tree and how many these paths? Two simple paths are different if the sets of nodes they go through are different.
 

Input
The first line has a number T (T <= 10), indicating the number of test cases.
For each test case, the first line contains three integers n, m, k(1<=n<=30000,1<=m<=60000,2<=k<=n), denote the number of nodes, the number of edges and the nodes of required paths.
Then next m lines, each lines contains three integers a, b, c(1<=a, b<=n, 1<=c<=10000),denote there is an edge between a, b and length is c.
 

Output
For each case, output two numbers, denote the length of required paths and the numbers of required paths.
 

Sample Input
  
  
1 6 6 4 1 2 1 2 3 1 3 4 1 2 5 1 3 6 1 5 6 1
 

Sample Output
  
  
3 4
 

Author
FZU
 

Source
 

这题和HYSBZ4016最短路径树问题一模一样,具体可以看我的HYSBZ4016的题解


#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <string>  
#include <algorithm>  
#include <cmath>  
#include <map>  
#include <cmath>  
#include <set>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <bitset>  
#include <functional>  

using namespace std;

#define LL long long  
const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 10;

int n, m, K, cnt1, cnt2;
char ch;
int s1[maxn], nt1[maxn], e1[maxn], v1[maxn];
int s2[maxn], nt2[maxn], e2[maxn], v2[maxn];
int vis[maxn], dis[maxn];
int cnt[maxn], mx[maxn], ans1, ans2;

int dfs(int k, int fa, int sum)
{
	int ans = mx[k] = 0;
	cnt[k] = 1;
	for (int i = s2[k]; i != -1; i = nt2[i])
	{
		if (vis[e2[i]] || e2[i] == fa) continue;
		int y = dfs(e2[i], k, sum);
		if (mx[y] < mx[ans]) ans = y;
		cnt[k] += cnt[e2[i]];
		mx[k] = max(mx[k], cnt[e2[i]]);
	}
	mx[k] = max(mx[k], sum - cnt[k]);
	return mx[k] < mx[ans] ? k : ans;
}

int deep(int k, int fa, int dep)
{
	if (dep == K) return K - 1;
	int ans = dep;
	mx[dep] = cnt[dep] = 0;
	for (int i = s2[k]; ~i; i = nt2[i])
	{
		if (vis[e2[i]] || e2[i] == fa) continue;
		ans = max(ans, deep(e2[i], k, dep + 1));
	}
	return ans;
}

void get(int k, int fa, int dep, int len, int sum)
{
	if (dep == K) return;
	if (K - 1 - dep <= sum && cnt[K - 1 - dep])
	{
		if (ans1 < len + mx[K - 1 - dep])
		{
			ans1 = len + mx[K - 1 - dep];
			ans2 = cnt[K - 1 - dep];
		}
		else if (ans1 == len + mx[K - 1 - dep]) ans2 += cnt[K - 1 - dep];
	}
	for (int i = s2[k]; ~i; i = nt2[i])
	{
		if (vis[e2[i]] || e2[i] == fa) continue;
		get(e2[i], k, dep + 1, len + v2[i], sum);
	}
}

void put(int k, int fa, int dep, int len, int sum)
{
	if (dep == K) return;
	if (mx[dep] < len) mx[dep] = len, cnt[dep] = 1;
	else if (mx[dep] == len) cnt[dep]++;
	for (int i = s2[k]; ~i; i = nt2[i])
	{
		if (vis[e2[i]] || e2[i] == fa) continue;
		put(e2[i], k, dep + 1, len + v2[i], sum);
	}
}

void Find(int k)
{
	int len = deep(k, k, 0);
	if (len + len + 1 < K) return;
	cnt[0] = 1;
	for (int i = s2[k]; ~i; i = nt2[i])
	{
		if (vis[e2[i]]) continue;
		get(e2[i], k, 1, v2[i], len);
		put(e2[i], k, 1, v2[i], len);
	}
}

void work(int k, int sum)
{
	mx[0] = INF;
	int y = dfs(k, k, sum);
	vis[y] = 1, Find(y);
	for (int i = s2[y]; ~i; i = nt2[i])
	{
		if (vis[e2[i]]) continue;
		if (cnt[e2[i]] < cnt[y]) work(e2[i], cnt[e2[i]]);
		else work(e2[i], sum - cnt[y]);
	}
}

struct Edge
{
	int u, v, w;
	bool operator<(const Edge &a)const
	{
		return u == a.u ? v > a.v:u < a.u;
	}
}p[maxn];

struct node
{
	int id, dis;
	bool operator<(const node& a)const
	{
		return dis == a.dis ? id > a.id : dis > a.dis;
	}
}pre, nt;

void dfs(int k)
{
	vis[k] = 0;
	for (int i = s1[k]; ~i; i = nt1[i])
	{
		if (dis[e1[i]] == dis[k] + v1[i])
		{
			if (!vis[e1[i]]) continue;
			nt2[cnt2] = s2[k], s2[k] = cnt2, e2[cnt2] = e1[i], v2[cnt2++] = v1[i];
			nt2[cnt2] = s2[e1[i]], s2[e1[i]] = cnt2, e2[cnt2] = k, v2[cnt2++] = v1[i];
			dfs(e1[i]);
		}
	}
}

void Dijkstra()
{
	priority_queue<node> q;
	memset(dis, INF, sizeof dis);
	memset(vis, 0, sizeof vis);
	pre = { 1,dis[1] = 0 };
	q.push(pre);
	while (!q.empty())
	{
		pre = q.top();
		q.pop();
		vis[pre.id] = 1;
		for (int i = s1[pre.id]; ~i; i = nt1[i])
		{
			if (vis[e1[i]]) continue;
			if (dis[e1[i]] > pre.dis + v1[i])
			{
				nt = { e1[i],dis[e1[i]] = pre.dis + v1[i] };
				q.push(nt);
			}
		}
	}
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d%d", &n, &m, &K);
		cnt1 = cnt2 = ans1 = ans2 = 0;
		memset(s1, -1, sizeof s1);
		for (int i = 0; i < m + m; i += 2)
		{
			scanf("%d%d%d", &p[i].u, &p[i].v, &p[i].w);
			p[i ^ 1] = p[i];
			swap(p[i ^ 1].v, p[i ^ 1].u);
		}
		sort(p, p + m + m);
		for (int i = 0; i < m + m; i++)
			nt1[cnt1] = s1[p[i].u], s1[p[i].u] = cnt1, e1[cnt1] = p[i].v, v1[cnt1++] = p[i].w;
		Dijkstra();
		memset(s2, -1, sizeof s2);
		dfs(1);
		work(1, n);
		printf("%d %d\n", ans1, ans2);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值