CodeChef:Two Closest(最短路 & 思维)
来源:互联网 发布:淘宝违法店铺举报 编辑:程序博客网 时间:2024/06/05 17:21
You are given a weighted graph with N nodes and M edges. Some of the nodes are marked as special nodes. Your task is to find the shortest pairwise distance between any two different special nodes.
Input
The first line of the input contains three space-separated integers N, M and K denoting the number of nodes, the number of edges, and the number of special nodes.
The following line contains K space-separated distinct integers A1, A2, ..., AK , denoting the special nodes.
Each of the following M lines (say, the jth) contains a triple Xj Yj Zj, denoting the edge connecting the nodes Xj and Yj, and having the weight of Zj.
Output
Output the shortest pairwise distance between any two different special nodes.
Constraints
- 2 ≤ K ≤ N
- The given graph is connected.
- The given graph doesn't contain self loops and multiple edges.
- 1 ≤ Ai ≤ N
- 1 ≤ Zj ≤ 104
- 1 ≤ Xj, Yj ≤ N
Subtasks
- Subtask #1 (20 points): 2 ≤ N ≤ 300, N-1 ≤ M ≤ N*(N-1)/2, 2 ≤ K ≤ N
- Subtask #2 (25 points): 2 ≤ N ≤ 105, N-1 ≤ M ≤ 105, 2 ≤ K ≤ 10
- Subtask #3 (55 points): 2 ≤ N ≤ 105, N-1 ≤ M ≤ 3 * 105, 2 ≤ K ≤ 104
Example
Input:5 5 31 3 51 2 32 3 43 4 14 5 81 5 19Output:7
Explanation
Nodes 1, 3 and 5 are special nodes. Shortest distance between nodes 1 and 3 is 7 and that between nodes 3 and 5 is 9. Shortest distance between nodes 1 and 5 is 16. Minimum of these distances is 7. Hence answer is 7.
题意:给一个无向图,K个特殊点,问这K个点中最近的两个点的距离。思路:特殊点任取一个作为起点跑dijk遇到另外的特殊点就结束,更新最短距离imin,从下一个特殊点开始继续跑,超过imin就break,显然复杂度O(NlogN)。
# include <bits/stdc++.h># define pb push_back# define mp make_pair# define pii pair<int,int># define A first# define B secondusing namespace std;const int maxn = 1e5+10;const int INF = 0x3f3f3f3f;int a[maxn], vis[maxn], dis[maxn];vector<pii >g[maxn];priority_queue<pii, vector<pii >, greater<pii > >q;int main(){ int t, n, m, k, u, v, w, cnt, imin = INF; scanf("%d%d%d",&n,&m,&k); for(int i=0; i<k; ++i) { scanf("%d",&t); a[t] = 1; } for(int i=0; i<m; ++i) { scanf("%d%d%d",&u,&v,&w); g[u].pb(mp(v,w)); g[v].pb(mp(u,w)); } for(int i=1; i<=n; ++i) { if(!a[i]) continue; for(int j=1; j<=n; ++j) dis[j] = INF, vis[j] = 0; cnt = dis[i] = 0; while(!q.empty()) q.pop(); q.push(mp(0, i)); while(!q.empty()) { int u = q.top().B; q.pop(); vis[u] = 1; cnt += a[u]; if(dis[u] > imin) break; if(cnt == 2) { imin = min(imin, dis[u]); break; } for(auto j : g[u]) { int v = j.A, w = j.B; if(vis[v]) continue; if(dis[v] > dis[u]+w) { dis[v] = dis[u]+w; q.push(mp(dis[v], v)); } } } } printf("%d\n",imin); return 0;}
- CodeChef:Two Closest(最短路 & 思维)
- Codeforces--333C--The Two Routes(最短路弗洛伊德)(思维)
- Codeforces 602C The Two Routes【最短路+思维】
- poj1548(最短路)-惯性思维
- hdu2433 最短路+思维
- 最短路 思维导图
- uva 10537 - The Toll! Revisited (逆向思维最短路)
- ZOJ 3710 Friends(思维,最短路,图论)
- HDU 6166 Senior Pan(思维 最短路)
- 【最短路 && 把一些点看成一个团】CodeChef
- CodeForces 602C The Two Routes(最短路)
- hdu 6181 Two Paths(最短路,spfa)
- CodeChef:Magic Board(思维 & 树状数组)
- CodeChef:Chef and Subsequences(思维 & dfs)
- 最短路(最短路)
- hrbust 1339 Touring【最短路+思维】
- FZU 2169 shadow【最短路+思维】
- SDUT 3562 Proxy【思维+最短路SPFA】
- QT 模板库、工具类和控件(三)
- iOS
- Linux 系统中用户切换
- RGB
- Android之四大组件、六大布局、五大存储
- CodeChef:Two Closest(最短路 & 思维)
- Initializing Spring root WebApplicationContext Debug Run as
- Oracle 12c 如何打开PDB可插播数据库
- nor flash函数分析
- Java必须会的Linux命令
- Android Studio虚拟机消失
- 小整数对象池,大整数对象池
- 一个员工的离职成本到底有多高?很恐怖!
- Junit4 简单使用及示例代码