CodeForcesGym 100676H Capital City
2015-08-22 20:56
351 查看
H. Capital City
Time Limit: 3000msMemory Limit: 262144KB
This problem will be judged on CodeForcesGym. Original ID: 100676H
64-bit integer IO format: %I64d Java class name: (Any)
Bahosain has become the president of Byteland, he is doing his best to make people's lives easier. Now, he is working on improving road networks between the cities.
If two cities are strongly connected, people can use BFS (Bahosain's Fast Service) to travel between them in no time. Otherwise, they have to follow one of the shortest paths between them, and of course, they will use BFS when they can! Two cities are connected if there is a path between them, and they are strongly connected if after removing any single road they will remain connected. President Bahosain wants to minimize the maximum distance people have to travel from any city to reach the capital city, can you help him in choosing the capital city?
Input
The first line of input contains one integer T, the number of test cases (1 ≤ T ≤ 64).
The first line of each test case contains two integers n, m (1 ≤ n ≤ 100,000) (0 ≤ m ≤ 200,000), the number of cities and the number of roads, respectively.
Each of the following m lines contains three space-separated integers a, b, c (1 ≤ a, b ≤ n) (1 ≤ c ≤
100,000), meaning that there is a road of length c connecting the cities a and b.
Byteland cities are connected since Bahosain became the president.
Test cases are separated with a blank line.
Output
For each test case, print the number of the city and length of the maximum shortest path on a
single line. If there is more than one possible city, print the one with the minimum number.
Sample Input
1
7 7
1 2 5
1 7 5
3 2 5
1 3 5
3 4 3
6 4 1
4 5 3
Sample Output
1 6
解题:边双连通分量 + 树的直径
#include <bits/stdc++.h> using namespace std; typedef long long LL; const LL INF = 0x3f3f3f3f3f3f3f3f; const int maxn = 100010; struct arc { int v,w,next; arc(int y = 0,int z = 0,int nxt = -1) { v = y; w = z; next = nxt; } bool operator<(const arc &t)const { return w < t.w; } } e[1000010]; int hd[maxn],hd2[maxn],low[maxn],dfn[maxn],belong[maxn],tot; void add(int *head,int u,int v,int w) { e[tot] = arc(v,w,head[u]); head[u] = tot++; e[tot] = arc(u,w,head[v]); head[v] = tot++; } int bcc,clk,n,m,uf[maxn]; stack<int>stk; int Find(int x) { if(x != uf[x]) uf[x] = Find(uf[x]); return uf[x]; } void tarjan(int u,int fa) { low[u] = dfn[u] = ++clk; stk.push(u); bool flag = false; for(int i = hd[u]; ~i; i = e[i].next) { if(!flag && e[i].v == fa) { flag = true; continue; } if(!dfn[e[i].v]) { tarjan(e[i].v,u); low[u] = min(low[u],low[e[i].v]); } else low[u] = min(low[u],dfn[e[i].v]); } if(low[u] == dfn[u]) { int v; ++bcc; do { v = stk.top(); stk.pop(); belong[v] = bcc; } while(v != u); } } LL d[2][maxn]; queue<int>q; int bfs(int u,int idx) { memset(d[idx],-1,sizeof d[idx]); while(!q.empty()) q.pop(); d[idx][u] = 0; q.push(u); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = hd2[u]; ~i; i = e[i].next) { if(d[idx][e[i].v] == -1) { d[idx][e[i].v] = d[idx][u] + e[i].w; q.push(e[i].v); } } } LL ret = -1; int id = 0; for(int i = 1; i <= bcc; ++i) if(ret < d[idx][i]) ret = d[idx][id = i]; return id; } void init() { for(int i = 0; i < maxn; ++i) { hd[i] = hd2[i] = -1; low[i] = dfn[i] = belong[i] = 0; uf[i] = i; } clk = tot = bcc = 0; while(!stk.empty()) stk.pop(); } int main() { int kase,u,v,w; scanf("%d",&kase); while(kase--) { init(); scanf("%d%d",&n,&m); for(int i = 0; i < m; ++i) { scanf("%d%d%d",&u,&v,&w); add(hd,u,v,w); } for(int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i,-1); if(bcc == 1) { puts("1 0"); continue; } for(int i = 1; i <= n; ++i) for(int j = hd[i]; ~j; j = e[j].next) { if(belong[i] == belong[e[j].v]) continue; add(hd2,belong[i],belong[e[j].v],e[j].w); } bfs(bfs(bfs(1,0),0),1); LL ret = INF; int id = 0; for(int i = 1; i <= n; ++i) { int bg = belong[i]; LL tmp = max(d[0][bg],d[1][bg]); if(tmp < ret) { ret = tmp; id = i; } } printf("%d %I64d\n",id,ret); } return 0; }
View Code
相关文章推荐
- 交互式shell和非交互式shell、登录shell和非登录shell的区别
- css--Bootstrap框架
- 编程算法 - 旋转矩阵 代码(C)
- mfc单文档程序对添加CEdit控件并操作
- spring 注解
- poj 1001高精浮点数乘法
- Codeforces Round #276 (Div. 1) B. Maximum Value(哈兮)
- 深入理解基于selenium的二次开发
- Spark里面:获取图Spark有多少行代码
- Exception in thread " " java.lang.UnsupportedClassVersionError: 错误
- C++ Vector 最大 最小值 索引 位置
- 白话空间统计十二:R语言对点数据分析的实现(1)
- unexpected end of file found in comment
- 编写高质量代码改善C#程序的157个建议——建议117:使用SSL确保通信中的数据安全
- Android Studio常见问题
- jsp页显示时间标签JSTL标签 实例大全
- Eclipse创建Struts项目
- (练习用)挑7
- c++_学习笔记0822
- Oracle Linux 7.1 安装oracle 12c RAC