[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=4093

[算法]

对于k个枢纽 , 分别在正向图和反向图上跑dijkstra最短路 , 即可

时间复杂度 : O(K(N + M) log N)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 20010
#define MAXK 210
typedef long long LL;
const LL inf = 1e15; int n , m , k , q , tot;
int heada[MAXN] , headb[MAXN];
LL dist1[MAXK][MAXN] , dist2[MAXK][MAXN];
bool visited[MAXK][MAXN]; struct edge
{
int to , w , nxt;
} e[MAXN << ]; template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedgea(int u , int v , int w)
{
tot++;
e[tot] = (edge){v , w , heada[u]};
heada[u] = tot;
}
inline void addedgeb(int u , int v , int w)
{
tot++;
e[tot] = (edge){v , w , headb[u]};
headb[u] = tot;
}
inline void dijkstra1(int idx,int s)
{
priority_queue< pair<int,int> > q;
for (int i = ; i <= n; i++) dist1[idx][i] = inf;
dist1[idx][s] = ;
q.push(make_pair( , s));
while (!q.empty())
{
int cur = q.top().second;
q.pop();
if (visited[idx][cur]) continue;
visited[idx][cur] = true;
for (int i = heada[cur]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (dist1[idx][cur] + w < dist1[idx][v])
{
dist1[idx][v] = dist1[idx][cur] + w;
q.push(make_pair(-dist1[idx][v] , v));
}
}
}
}
inline void dijkstra2(int idx,int s)
{
priority_queue< pair<int,int> > q;
for (int i = ; i <= n; i++) dist2[idx][i] = inf;
dist2[idx][s] = ;
q.push(make_pair( , s));
while (!q.empty())
{
int cur = q.top().second;
q.pop();
if (visited[idx][cur]) continue;
visited[idx][cur] = true;
for (int i = headb[cur]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (dist2[idx][cur] + w < dist2[idx][v])
{
dist2[idx][v] = dist2[idx][cur] + w;
q.push(make_pair(-dist2[idx][v] , v));
}
}
}
} int main()
{ read(n); read(m); read(k); read(q);
for (int i = ; i <= m; i++)
{
int u , v , w;
read(u); read(v); read(w);
addedgea(u , v , w);
addedgeb(v , u , w);
}
for (int i = ; i <= k; i++)
{
int x;
read(x);
memset(visited[i] , false , sizeof(visited[i]));
dijkstra1(i , x);
memset(visited[i] , false , sizeof(visited[i]));
dijkstra2(i , x);
}
int ans1 = , ans2 = ;
while (q--)
{
int x , y;
read(x); read(y);
LL tmp = inf;
for (int i = ; i <= k; i++) chkmin(tmp , 1LL * dist2[i][x] + 1LL * dist1[i][y]);
if (tmp != inf)
{
++ans1;
ans2 += (LL)tmp;
}
}
printf("%d\n%d\n" , ans1 , ans2);
return ;
}

最新文章

  1. 【转】C# GET 和 SET作用
  2. HTTP网页错误代码大全带解释
  3. ThinkPad X220i 安装 Mac OSX
  4. VMware Workstation CentOS-6.4-x86_64-minimal 配置网络以及安装JDK和tomcat
  5. MySql Connector/Net Mysql like 搜索中文的问题(c#和asp.net连接mysql)
  6. QT中共享库的生成与使用
  7. 【Xamarin 开发 IOS --IOS 页面导航概念Segue】
  8. 索引列上的统计 &lt;第一篇&gt;
  9. ucos信号量集源码分析
  10. locate/slocate命令
  11. 中文转unicode,中文转bytes,unicode转bytes java实现
  12. python相关资料
  13. 深入.NET数据类型(2)
  14. 【转】GPS网平差
  15. 流量控制闸门——LimitLatch套接字连接数限制器
  16. Pycharm使用教程(四)-安装python依赖包(非常详细,非常实用)
  17. linux下objdump应用
  18. ES6最新语法
  19. SQL关于WHERE 的计算次序
  20. su: cannot set user id: Resource temporarily unavailable【转】

热门文章

  1. POJ-2773 Happy 2006,暴力2700ms+水过!
  2. Codeforces603E - Pastoral Oddities
  3. hdu 3879 最大权闭合图(裸题)
  4. 【BZOJ1061】志愿者招募(单纯形,对偶性)
  5. 【转载】js中对象的使用
  6. navicat 无法连接到腾讯云Mysql
  7. java获得文件的最后修改时间
  8. zerorpc使用时报错:No handlers could be found for logger &quot;zerorpc.channel&quot;
  9. 将oracle10g 升级至10.2.0.4
  10. c++多线程编程:常见面试题