Shortest-path tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 130712/130712 K (Java/Others)Total Submission(s): 1532 Accepted Submission(s): 498
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.
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.
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
Recommend
【分析】
没想到2A了...第一次思路挂了所以没A,思路调整一下啪啪啪写完直接交就A掉了...突然一阵莫名喜悦。
两个子问题:1.求最短路条件下的字典序最小生成树。2.在生成树上选取一条包含K个点的路径,最大化路径权值和,在最大化路径权值和的基础上,询问有多少条不同的路径。
问题1:跑一遍spfa,对于每个节点相连的节点,先把他们从小到大排序再加入邻接表,保证dfs的时候先dfs到的点字典序较小。所以这个问题spfa+dfs就可以解决= =
问题2:树的点分治。用len[i]表示当前包含i个节点(不包括根节点)的路径的最大值,sum[i]表示在len[i]的基础上这样的路径有多少条。对于每一棵子树,要先更新答案,再更新sum和len两个数组。
【代码】
//hdu 4871
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 1e9+7
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
const int mxn=60005;
queue <int> q;
bool vis[mxn];
int nu[mxn],nv[mxn],nw[mxn];
struct node {int u,v,w;} e[mxn<<1];
struct edge {int to,w,next;} f[mxn<<1];
int n,m,K,T,mn,cnt,tot,num,ans,root,pos,mxdep;
int head[mxn],size[mxn],dis[mxn],mx[mxn],sum[mxn],len[mxn];
inline bool comp(node x,node y)
{
return x.u==y.u?x.v>y.v:x.u<y.u;
}
inline void add(int u,int v,int w)
{
f[++cnt].to=v,f[cnt].w=w,f[cnt].next=head[u],head[u]=cnt;
}
inline void dfssize(int u,int fa)
{
size[u]=1,mx[u]=0;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v] || v==fa) continue;
dfssize(v,u);
size[u]+=size[v];
mx[u]=max(mx[u],size[v]);
}
}
inline void dfsroot(int r,int u,int fa)
{
mx[u]=max(mx[u],size[r]-size[u]);
if(mx[u]<mn) mn=mx[u],root=u;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v] || v==fa) continue;
dfsroot(r,v,u);
}
}
inline void dfsdis(int u,int fa,int dep)
{
if(dep>=K) return;
if(dis[u]+len[K-dep-1]>ans)
ans=dis[u]+len[K-dep-1],pos=sum[K-dep-1];
else if(dis[u]+len[K-dep-1]==ans)
pos+=sum[K-dep-1];
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v] || v==fa) continue;
dis[v]=dis[u]+f[i].w;
dfsdis(v,u,dep+1);
}
}
inline void update(int u,int fa,int dep)
{
if(dep>=K) return;
mxdep=max(mxdep,dep);
if(dis[u]>len[dep])
len[dep]=dis[u],sum[dep]=1;
else if(dis[u]==len[dep])
sum[dep]++;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v] || v==fa) continue;
update(v,u,dep+1);
}
}
inline void calc(int u)
{
mxdep=0;
len[0]=0,sum[0]=1;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v]) continue;
dis[v]=f[i].w;
dfsdis(v,u,1);
update(v,u,1);
}
fo(i,1,mxdep) len[i]=-inf;
}
inline void dfs(int u)
{
mn=n;
dfssize(u,0);
dfsroot(u,u,0);
vis[root]=1;
calc(root);
for(int i=head[root];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v]) continue;
dfs(v);
}
}
inline void spfa()
{
memset(vis,0,sizeof vis);
memset(dis,0x3f,sizeof dis);
q.push(1),dis[1]=0;
while(!q.empty())
{
int u=q.front();
q.pop();vis[u]=0;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to,w=f[i].w;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(!vis[v]) vis[v]=1,q.push(v);
}
}
}
}
inline void build(int u)
{
vis[u]=1;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to,w=f[i].w;
if(!vis[v] && dis[v]==dis[u]+w)
build(v),nu[++num]=u,nv[num]=v,nw[num]=w;
}
}
int main()
{
int u,v,w;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&K);
M(head),cnt=tot=num=ans=pos=0;
fo(i,1,n) len[i]=-inf;
fo(i,1,m)
{
scanf("%d%d%d",&u,&v,&w);
e[++tot]=(node){u,v,w};
e[++tot]=(node){v,u,w};
}
sort(e+1,e+tot+1,comp);
fo(i,1,tot) add(e[i].u,e[i].v,e[i].w);
spfa();
build(1);
M(head),M(vis),cnt=0;
fo(i,1,num)
add(nu[i],nv[i],nw[i]),add(nv[i],nu[i],nw[i]);
dfs(1);
printf("%d %d\n",ans,pos);
}
return 0;
}