2014多校一1011(HDU4871)--Shortest-path tree树分治
来源:互联网 发布:三菱cnc m70 数据采集 编辑:程序博客网 时间:2024/06/10 17:06
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
16 6 41 2 12 3 13 4 12 5 13 6 15 6 1
Sample Output
3 4思路:没接触过树分治先看漆子超论文、、堆优化的DIJK跑一下得到所求的最短路径树。然后每次找到树重心,那么K个点的路径分为经过重心和没经过重心两种。没经过重心的递归处理。经过重心的每跑一个点,如果当前点到重心的步数为a,就和之前子树的dis[k-1-a]的最大值相加,不断更新最大值和维护出现次数。每颗子树跑完再更新dis。#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <map>#include <set>#include <cmath>#include <queue>#include <string>using namespace std;#define maxn 200800#define inf 0x3f3f3f3fint first[maxn];int vv[maxn],nxt[maxn],ww[maxn],dd[maxn],size[maxn],cnt[maxn],val[maxn],dp[maxn],dis[maxn],num[maxn],fuck,fuckyou;bool vis[maxn];int e,nowsize,maxsonsize,Center,k;void init(){e = fuck = fuckyou = 0;memset(val,0,sizeof(val));memset(vis,0,sizeof(vis));memset(first,-1,sizeof(first));for(int i = 1;i <= k;i++)dis[i] = -inf;}struct Edge{int v,cost;Edge(){}Edge(int v_,int c_){v = v_;cost = c_;}};vector <Edge> ans[maxn];void addedge(int u,int v,int w){vv[e] = v;nxt[e] = first[u];ww[e] = w;first[u] = e++;vv[e] = u;nxt[e] = first[v];ww[e] = w;first[v] = e++;}struct Node{int pre,u,d,cost;Node(){}Node(int pp,int uu,int dd,int cc){pre = pp;u = uu;d = dd;cost = cc;}bool operator < (const Node & a)const{if(d != a.d)return d > a.d;else if(pre != a.pre)return pre > a.pre;else return u > a.u;}};priority_queue <Node> q;void Dijk(int n){memset(dd,0x3f,sizeof(dd));dd[1] = 0;while(!q.empty())q.pop();q.push(Node(-1,1,0,0));int nown = 0;while(nown < n && !q.empty()){L:Node node = q.top();q.pop();if(vis[node.u] && !q.empty())goto L;if(!vis[node.u]){nown++;vis[node.u] = 1;addedge(node.pre,node.u,node.cost);for(int i = 0;i < ans[node.u].size();i++){int v = ans[node.u][i].v,w = ans[node.u][i].cost;if(!vis[v] && dd[node.u] + w <= dd[v]){dd[v] = dd[node.u] + w;q.push(Node(node.u,v,dd[v],w));}}}}}vector <int> D[maxn];void dfs(int u,int pre){nowsize++;cnt[u] = 1;dp[u] = 0;for(int i = first[u];i != -1;i = nxt[i]){int v = vv[i];if(v == pre || v==-1 || val[v])continue;dfs(v,u);cnt[u] += cnt[v];dp[u] = max(dp[u],cnt[v]);}}void get_center(int u,int fa){int tmp = max(nowsize-cnt[u],dp[u]);if(tmp < maxsonsize){Center = u;maxsonsize = tmp;}for(int i = first[u];i != -1;i = nxt[i]){int v = vv[i];if(v == fa || v==-1 || val[v])continue;get_center(v,u);}}void Get_Center(int u,int pre){Center = u,maxsonsize = inf,nowsize = 0;dfs(u,pre);get_center(u,pre);}void DFS(int u,int pre,int d,int dd){D[d].push_back(dd);if(d > k)return;if(dd+dis[k-d] > fuck){fuck = dd+dis[k-d];fuckyou = 1;}else if(dd+dis[k-d]==fuck)fuckyou+=num[k-d];for(int i = first[u];i != -1;i = nxt[i]){int v = vv[i];if(v == pre || v==-1 || val[v])continue;DFS(v,u,d+1,dd+ww[i]);}}void gao(int u,int pre){Get_Center(u,pre);//得到树的重心Centerif(nowsize <= k+1)return;for(int i = 0;i <= k+1;i++)D[i].clear();memset(dis,0,sizeof(dis));memset(num,0,sizeof(num));val[Center] = 1;for(int i = first[Center];i != -1;i = nxt[i]){int v = vv[i];if(v==-1 || val[v])continue;DFS(v,Center,1,ww[i]);for(int i = 0;i <= k;i++){for(int j = 0;j < D[i].size();j++){int a = D[i][j];if(a > dis[i]){dis[i] = a;num[i] = 1;}else if(a == dis[i])num[i]++;}D[i].clear();}}int aa = Center;for(int i = first[aa];i != -1;i = nxt[i]){int v = vv[i];if(v==-1 || val[v]) continue;gao(v,aa);}}int main(){//freopen("in.txt","r",stdin);int t;scanf("%d",&t);while(t--){int n,m;scanf("%d%d%d",&n,&m,&k);for(int i = 1;i <= n;i++)ans[i].clear();while(m--){int u,v,w;scanf("%d%d%d",&u,&v,&w);ans[u].push_back(Edge(v,w));ans[v].push_back(Edge(u,w));}init();k--;Dijk(n);//建立好最段路径了,接下来是搜一下这棵树gao(1,-1);printf("%d %d\n",fuck,fuckyou);}return 0;}
0 0
- 2014多校一1011(HDU4871)--Shortest-path tree树分治
- hdu4871 Shortest-path tree
- HDU4871-Shortest-path tree
- HDU4871 Shortest-path tree【最短路+点分治】
- hdu 4871 Shortest-path tree 树分治
- [HDOJ 4871] Shortest-path tree [树的分治+动态规划]
- HDU 4871 Shortest-path tree (树分治+SPFA)
- 2014多校(1011)hdu4871
- HDU 4871 Shortest-path tree (树分治之点分治 最短路径树问题)
- 最短路+树分治(hdu4871多校联合)
- HDU 4871 Shortest-path tree 最短路+树的点分治
- hdu 4871 Shortest-path tree(最短路+点分治)
- 【HDU】4871 Shortest-path tree 最短路+点分治
- HDU 4871 Shortest-path tree (最短路+树上点分治)
- hdu 4871 Shortest-path tree
- HDU 4871Shortest-path tree
- hdu 4871 Shortest-path tree
- HDU3631(Shortest Path)
- 如何用javac 和java 编译运行整个Java工程
- POJ-1611(并查集)
- 的撒谎时突然看见的与客户的天空货款
- C++中模板使用详解
- sublime3 c++环境配置,可cmd输入,安装插件管理 Package Control,乱码
- 2014多校一1011(HDU4871)--Shortest-path tree树分治
- 【同步策略】位置同步策略和AOI
- hdu 4869 Turn the pokers
- OpenStack Nova 的存储管理
- 你指定顶点=绿色
- PHP自学,第一阶段,基础学习
- VC6.0 控件Radio Button的使用
- POJ 3020:Antenna Placement:无向图的最小边覆盖
- android 开启闪光灯