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


同Bzoj4016

#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 0x7FFFFFFF;
const int maxn = 2e5 + 10;
int n, m, x, y, z, K, T;
char ch;

struct Tree
{
int ft[maxn], nt[maxn], u[maxn], v[maxn], sz, n;
int vis[maxn], cnt[maxn], mx[maxn], a, b;
void clear(int n)
{
this->n = n;
a = b = sz = 0;
for (int i = 1; i <= n; i++)
{
ft[i] = -1;
vis[i] = 0;
}
}
void AddEdge(int x, int y, int z)
{
u[sz] = y; v[sz] = z;
nt[sz] = ft[x]; ft[x] = sz++;
}
int dfs(int x, int fa, int sum)
{
int ans = mx[x] = 0;
cnt[x] = 1;
for (int i = ft[x]; i != -1; i = nt[i])
{
if (vis[u[i]] || u[i] == fa) continue;
int y = dfs(u[i], x, sum);
if (mx[y]<mx[ans]) ans = y;
cnt[x] += cnt[u[i]];
mx[x] = max(mx[x], cnt[u[i]]);
}
mx[x] = max(mx[x], sum - cnt[x]);
return mx[x] < mx[ans] ? x : ans;
}
int deep(int x, int fa, int dep)
{
if (dep == K) return K - 1;
int ans = dep;
mx[dep] = cnt[dep] = 0;
for (int i = ft[x]; i != -1; i = nt[i])
{
if (vis[u[i]] || u[i] == fa) continue;
ans = max(ans, deep(u[i], x, dep + 1));
}
return ans;
}
void get(int x, int fa, int dep, int len, int kind, int lit)
{
if (dep == K) return;
if (!kind)
{
if (K - 1 - dep <= lit && cnt[K - 1 - dep])
{
if (a<len + mx[K - 1 - dep])
{
a = len + mx[K - 1 - dep];
b = cnt[K - 1 - dep];
}
else if (a == len + mx[K - 1 - dep]) b += cnt[K - 1 - dep];
}
}
else
{
if (mx[dep]<len) mx[dep] = len, cnt[dep] = 1;
else if (mx[dep] == len) cnt[dep]++;
}
for (int i = ft[x]; i != -1; i = nt[i])
{
if (vis[u[i]] || u[i] == fa) continue;
get(u[i], x, dep + 1, len + v[i], kind, lit);
}
}
void find(int x)
{
int len = deep(x, -1, 0);
if (len + len + 1 < K) return;
cnt[0] = 1;
for (int i = ft[x]; i != -1; i = nt[i])
{
if (vis[u[i]]) continue;
get(u[i], x, 1, v[i], 0, len);
get(u[i], x, 1, v[i], 1, len);
}
}
void work(int x, int sum)
{
mx[0] = INF;
int y = dfs(x, -1, sum);
vis[y] = 1; find(y);
for (int i = ft[y]; i != -1; i = nt[i])
{
if (vis[u[i]]) continue;
if (cnt[u[i]]<cnt[y]) work(u[i], cnt[u[i]]);
else work(u[i], sum - cnt[y]);
}
}
}solve;

struct DAG
{
int ft[maxn], nt[maxn], u[maxn], v[maxn], sz, n;
int dis[maxn], vis[maxn];
struct point
{
int x, y;
point(int x = 0, int y = 0) :x(x), y(y) {}
bool operator<(const point& a)const
{
return y == a.y ? x > a.x : y >a.y;
}
};
void clear(int n)
{
this->n = n; sz = 0;
for (int i = 1; i <= n; i++)
{
ft[i] = dis[i] = -1;
vis[i] = 0;
}
}
void AddEdge(int x, int y, int z)
{
u[sz] = y; v[sz] = z;
nt[sz] = ft[x]; ft[x] = sz++;
}
void dfs(int x)
{
vis[x] = 0;
for (int i = ft[x]; i != -1; i = nt[i])
{
if (dis[u[i]] == dis[x] + v[i])
{
if (!vis[u[i]]) continue;
solve.AddEdge(x, u[i], v[i]);
solve.AddEdge(u[i], x, v[i]);
dfs(u[i]);
}
}
}
void dijkstra()
{
priority_queue<point> p;
p.push(point(1, dis[1] = 0));
while (!p.empty())
{
point q = p.top(); p.pop();
if (vis[q.x]) continue; else vis[q.x] = 1;
for (int i = ft[q.x]; i != -1; i = nt[i])
{
if (dis[u[i]] == -1 || dis[u[i]] > q.y + v[i])
{
p.push(point(u[i], dis[u[i]] = q.y + v[i]));
}
}
}
solve.clear(n);
dfs(1);
solve.work(1, n);
printf("%d %d\n", solve.a, solve.b);
}
}dag;

void read(int &x)
{
while ((ch = getchar()) < '0' || ch > '9');
x = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') x = x * 10 + ch - '0';
}

struct point
{
int x, y, z;
void scan() { read(x); read(y); read(z); }
bool operator<(const point &a)const
{
return x == a.x ? y > a.y:x < a.x;
}
}p[maxn];

int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &n, &m, &K);
dag.clear(n);
for (int i = 0; i < m + m; i += 2)
{
p[i].scan();
p[i ^ 1] = p[i];
swap(p[i ^ 1].x, p[i ^ 1].y);
}
sort(p, p + m + m);
for (int i = 0; i < m + m; i++)
{
dag.AddEdge(p[i].x, p[i].y, p[i].z);
}
dag.dijkstra();
}
return 0;
}



温故而知新,发现之前代码中的一个bug,满足条件的情况是可能超出int范围的

#include<map>
#include<ctime>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)
typedef long long LL;
typedef pair<int, int> pii;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int M = 5e6;
const double eps = 1e-10;
int T, n, m, L, x, y, z;
int ft[N], nt[N], u[N], v[N], sz;
int dis[N];
int vis[N], mx[N], ct[N];
int f[N], cf[N], g[N], cg[N];
int a; LL b;
vector<pii> t[N];

int get(int x, int fa, int n)
{
int y = mx[x] = 0; ct[x] = 1;
loop(i, ft[x], nt)
{
if (u[i] == fa || vis[u[i]]) continue;
int z = get(u[i], x, n);
ct[x] += ct[u[i]];
mx[x] = max(mx[x], ct[u[i]]);
y = mx[y] < mx[z] ? y : z;
}
mx[x] = max(mx[x], n - ct[x]);
return mx[x] < mx[y] ? x : y;
}

void work(int x, int fa, int V, int d, int &D)
{
if (d > D) ++D, g[d] = V, cg[d] = 1;
else if (g[d] < V) g[d] = V, cg[d] = 1;
else if (g[d] == V) cg[d]++;
loop(i, ft[x], nt)
{
if (vis[u[i]] || u[i] == fa) continue;
work(u[i], x, V + v[i], d + 1, D);
}
}

void solve(int x)
{
int len = 1; f[len] = 0, cf[len] = 1;
loop(i, ft[x], nt)
{
if (vis[u[i]]) continue;
int size = 0;
work(u[i], x, v[i], 1, size);
rep(j, 1, size)
{
if (L - j < 1 || L - j > len) continue;
if (a < f[L - j] + g[j]) a = f[L - j] + g[j], b = 1LL * cf[L - j] * cg[j];
else if (a == f[L - j] + g[j]) b += 1LL * cf[L - j] * cg[j];
}
rep(j, 1, size)
{
if (len < j + 1) ++len, f[len] = g[j], cf[len] = cg[j];
else if (f[j + 1] < g[j]) f[j + 1] = g[j], cf[j + 1] = cg[j];
else if (f[j + 1] == g[j]) cf[j + 1] += cg[j];
}
}
}

void dfs(int x, int n)
{
int y = get(x, x, n);
vis[y] = 1; solve(y);
loop(i, ft[y], nt)
{
if (vis[u[i]]) continue;
dfs(u[i], ct[u[i]] < ct[y] ? ct[u[i]] : n - ct[y]);
}
}

struct cmp
{
bool operator()(pii&a, pii&b) { return a > b; }
};

void build(int x)
{
vis[x] = 1;
sort(t[x].begin(), t[x].end());
for (int i = 0; i < t[x].size(); i++)
{
pii j = t[x][i];
if (vis[j.first] || dis[x] + j.second != dis[j.first]) continue;
u[sz] = j.first; v[sz] = j.second; nt[sz] = ft[x]; ft[x] = sz++;
u[sz] = x; v[sz] = j.second; nt[sz] = ft[j.first]; ft[j.first] = sz++;
build(j.first);
}
}

int main()
{
for (inone(T); T; T--)
{
inthr(n, m, L);
mx[sz = 0] = INF; a = b = 0;
rep(i, 1, n) ft[i] = -1, dis[i] = INF, vis[i] = 0, t[i].clear();
rep(i, 1, m)
{
inthr(x, y, z);
t[x].push_back(make_pair(y, z));
t[y].push_back(make_pair(x, z));
}
priority_queue<pii, vector<pii>, cmp> p;
p.push(make_pair(dis[1] = 0, 1));
while (!p.empty())
{
pii q = p.top(); p.pop();
for (auto i : t[q.second])
{
if (dis[i.first] <= q.first + i.second) continue;
p.push(make_pair(dis[i.first] = q.first + i.second, i.first));
}
}
build(1);
rep(i, 1, n) vis[i] = 0;
dfs(1, n);
printf("%d %lld\n", a, b);
}
return 0;
}