最短路+树分治(hdu4871多校联合)

Shortest-path tree

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


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


题意:给一个无向连通图,要做两件事情。1.建立以1为根的树,是的树上的每个节点到1的距离等于原来图上的最短路,并且字典序最小;2.求树上 恰好有k个点的路径有多少,要求距离最长

思路:首先Dijkstra求出最短路,并且建立最小生成树,也就是在求最短路的时候建图,那么只需要在每个节点出队列的时候,加边就可以了;然后就是树分治了,dis保存距离当前重心距离为i的最长路径是多长,num表示这么长的路径有多少条,那么每次在dfsdis的时候更新答案,dfsdis之后,更新dis和num

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=30810;
const int INF=0x3f3f3f3f;
int N,M,K;
int ans1,ans2;
struct node
{
    int v,next,w;
    node(){}
    node(int vi,int wi):v(vi),w(wi){}
}edge[maxn*2];
int dis[maxn],num[maxn];
vector<int> D[maxn];
int Max,root;
int head[maxn],tot;
vector<node> E[maxn];
int dis1[maxn];
int vis[maxn];
int size[maxn];
int maxv[maxn];
int nowsize;

void add_edge(int u,int v,int w)
{
    edge[tot].v=v;
    edge[tot].w=w;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void init()
{
    tot=0;
    ans1=ans2=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=N;i++)
        E[i].clear();
}
struct Node
{
    int f,u,d,w;
    Node(){}
    Node(int a,int b,int c,int e)
    {
        f=a;u=b;
        d=c;w=e;
    }
    //保证字典序最小
    bool operator<(const Node &a)const
    {
        if(d!=a.d)return d>a.d;
        if(f!=a.f)return f>a.f;
        return u>a.u;
    }
};
priority_queue<Node> q;
void Dij(int st)
{
    for(int i=0;i<=N;i++)
        dis1[i]=INF;
    dis1[st]=0;
    while(!q.empty())q.pop();
    q.push(Node(-1,1,0,0));
    int cnt=0;
    while(cnt<N&&!q.empty())
    {
        Node tmp=q.top();
        q.pop();
        int u=tmp.u;
        if(vis[u])continue;
        cnt++;
        add_edge(tmp.f,tmp.u,tmp.w);
        add_edge(tmp.u,tmp.f,tmp.w);
        vis[u]=1;
        int len=E[u].size();
        for(int i=0;i<len;i++)
        {
            int v=E[u][i].v,w=E[u][i].w;
            if(!vis[v]&&dis1[v]>=dis1[u]+w)
            {
                dis1[v]=dis1[u]+w;
                q.push(Node(u,v,dis1[v],w));
            }
        }
    }
}
void dfssize(int u,int f)
{
    nowsize++;
    size[u]=1;
    maxv[u]=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==-1||v==f||vis[v])continue;
        dfssize(v,u);
        size[u]+=size[v];
        if(size[v]>maxv[u])maxv[u]=size[v];
    }
}
void dfsroot(int r,int u,int f)
{
    if(size[r]-size[u]>maxv[u])
        maxv[u]=size[r]-size[u];
    if(maxv[u]<Max)Max=maxv[u],root=u;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==-1||v==f||vis[v])continue;
        dfsroot(r,v,u);
    }
}

void dfsdis(int u,int fa,int d,int w)
{
    D[d].push_back(w);
    if(d>K)return;
    if(w+dis[K-d]>ans1)
    {
        ans1=w+dis[K-d];
        ans2=1;
    }
    else if(w+dis[K-d]==ans1)ans2+=num[K-d];
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v]&&v!=fa&&v!=-1)
            dfsdis(v,u,d+1,w+edge[i].w);
    }
}
void solve(int u,int fa)
{
    Max=INF;
    nowsize=0;
    dfssize(u,fa);
    dfsroot(u,u,fa);
    if(nowsize<=K+1)return;//这可子树的节点数小于K+1了,就不用往下走了
    for(int i=0;i<=K+1;i++)D[i].clear();
    memset(dis,0,sizeof(dis));
    memset(num,0,sizeof(num));
    vis[root]=1;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==-1||vis[v]||v==fa)continue;
        dfsdis(v,root,1,edge[i].w);
        for(int i=0;i<=K;i++)
        {
            for(int j=0;j<D[i].size();j++)
            {
                int tmp=D[i][j];
                if(tmp>dis[i])
                    dis[i]=tmp,num[i]=1;
                else if(tmp==dis[i])num[i]++;
            }
            D[i].clear();
        }
    }
    int tmp=root;
    for(int i=head[tmp];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==-1||vis[v]||v==fa)continue;
        solve(v,tmp);
    }
}
int main()
{
    int T,u,v,w;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&N,&M,&K);
        init();
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            E[u].push_back(node(v,w));
            E[v].push_back(node(u,w));
        }
        Dij(1);
        K--;
        memset(vis,0,sizeof(vis));
        solve(1,-1);
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值