HDU 4871 Shortest-path tree(最短路 + 点分治,大致参考 hdu 4812)

Shortest-path tree

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

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

题意: 一个图的最短路径生成树是,以 1 为根节点,生成树中 1 到每个点的距离都是原图中 1 到每个点的最短距离 ,若有多个最短取字典序最小的一个。求此生成树下,一条包含 k 个节点的路径的长度最大值以及这个最大值的个数、

Analyse: 首先是最短路径生成树的问题,首先用 dijkstra 处理 1 到所有点的最短距离,由于需要字典序最小,所以直接按节点的编号由小到大 dfs,若满足 dis[v] == dis[u] + w,那么这条边就是生成树中的一条边,接着 dfs (v) 即可、

其此是处理答案的问题,这个做法完全可以参考 hdu 4812 .
有一个比较坑的地方就是代码中 dfs 处理子树时的一个 vis[v] == 1 则 continue 的优化。再 hdu 4812 中

不加这一句话也可以过,时间是 3000ms 左右,然而在此题中不加这个优化会直接 TLE.

加了优化之后 hdu 4812 是 2000ms AC,然而在此题中直接 500ms AC (在代码中标出了)

Code:

#include<bits/stdc++.h>
#define debug(x) cerr << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;
inline LL read() {
    LL s = 0,w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-')
            w = -1;
        ch = getchar();
    }
    while(isdigit(ch))
        s = s * 10 + ch - '0',ch = getchar();
    return s * w;
}
inline void write(LL x) {
    if(x < 0)
        putchar('-'), x = -x;
    if(x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
#ifndef ONLINE_JUDGE
clock_t prostart = clock();
#endif

const int maxn = 3e5 + 10;
const int inf = 0x3f3f3f3f;
int n,m,k;
struct xx{
    int v,w,nex;
}edge[maxn << 1];       //存树
int head[maxn],cnt;
vector<pii> e[maxn];    //存图
int dis[maxn],ans,ansc;
bool tag[maxn],vis[maxn];

void init(){
    for(int i = 0;i < n + 5;++ i){
        head[i] = -1;
        e[i].clear();
        vis[i] = tag[i] = false;
        dis[i] = inf;
    }
    ans = ansc = cnt = 0;
}

void dij(int s){
    dis[s] = 0;
    priority_queue<pii> Q;
    Q.push(MP(-dis[s],s));
    while(!Q.empty()){
        int now = Q.top().second; Q.pop();
        for(int i = 0;i < e[now].size();++ i){
            int v = e[now][i].first;
            int w = e[now][i].second;
            if(dis[v] > dis[now] + w){
                dis[v] = dis[now] + w;
                Q.push(MP(-dis[v],v));
            }
        }
    }
}

void solve_tree(int p){
    tag[p] = 1;
    for(int i = 0;i < e[p].size();++ i){
        int v = e[p][i].first;
        int w = e[p][i].second;
        if(tag[v]) continue;
        if(dis[v] == dis[p] + w){
            edge[cnt] = xx{v,w,head[p]};
            head[p] = cnt ++;
            edge[cnt] = xx{p,w,head[v]};
            head[v] = cnt ++;
            solve_tree(v);
        }
    }
}

int sonnum[maxn],sonmax[maxn];

void Tree_size(int p,int fa){
    sonnum[p] = sonmax[p] = 1;
    for(int i = head[p];i != -1;i = edge[i].nex){
        int v = edge[i].v;
        if(v == fa || vis[v]) continue;
        Tree_size(v,p);
        sonnum[p] += sonnum[v];
        sonmax[p] = max(sonmax[p],sonnum[v]);
    }
}

void Tree_center(int &mi,int &rt,int p,int fa,int sum){
    for(int i = head[p];i != -1;i = edge[i].nex){
        int v = edge[i].v;
        if(v == fa || vis[v]) continue;
        Tree_center(mi,rt,v,p,sum);
    }
    if(mi > max(sonmax[p],sum - sonnum[p])){
        mi = max(sonmax[p],sum - sonnum[p]);
        rt = p;
    }
}

int flag[maxn],ccoun[maxn];
int e1[maxn],e2[maxn],p1,p2,maxx[maxn];

void update(int &aa,int bb,int &cc,int val){
    if(bb > aa){
        aa = bb;
        cc = 1;
    }
    else if(aa == bb) cc += val;
}

void Tree_dis(int p,int fa,int dep,int d){      ///dep 深度,即包含节点数,d 距离
    if(dep == k){
        update(ans,d,ansc,1);
        return ;
    }
    if(flag[k - dep + 1]){
        update(ans,d + flag[k - dep + 1],ansc,ccoun[k - dep + 1]);
    }
    e1[++ p1] = dep;
    e2[++ p2] = dep;
    maxx[p1] = d;
    for(int i = head[p];i != -1;i = edge[i].nex){
        int v = edge[i].v;
        int w = edge[i].w;
        if(v == fa || vis[v]) continue;
        Tree_dis(v,p,dep + 1,d + w);
    }
}

void dfs(int p,int fa){
    int mi = inf,rt;
    Tree_size(p,fa);
    Tree_center(mi,rt,p,fa,sonnum[p]);
    vis[rt] = 1;
    p2 = 0;
    for(int i = head[rt];i != -1;i = edge[i].nex){
        p1 = 0;
        int v = edge[i].v;
        if(vis[v]) continue;						/// 不加直接 T
        Tree_dis(v,rt,2,edge[i].w);
        for(int j = 1;j <= p1;++ j){
            update(flag[e1[j]],maxx[j],ccoun[e1[j]],1);
        }
    }
    for(int i = 1;i <= p2;++ i){
        flag[e2[i]] = 0;
        ccoun[e2[i]] = 0;
    }
    for(int i = head[rt];i != -1;i = edge[i].nex){
        int v = edge[i].v;
        if(vis[v] || v == fa) continue;
        dfs(v,rt);
    }
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif

    int t; scanf("%d",&t);
    while(t --){
        scanf("%d%d%d",&n,&m,&k);
        init();
        for(int i = 1;i <= m;++ i){
            int u,v,w; scanf("%d%d%d",&u,&v,&w);
            e[u].pb(MP(v,w));
            e[v].pb(MP(u,w));
        }
        dij(1);
        for(int i = 1;i <= n;++ i){
            sort(e[i].begin(),e[i].end());
        }
        solve_tree(1);
        dfs(1,-1);
        printf("%d %d\n",ans,ansc);
    }

#ifndef ONLINE_JUDGE
//    cerr << "time: " << 1.0 * (clock() - prostart) / CLOCKS_PER_SEC << " s" << endl;
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值