题目链接:http://codeforces.com/contest/667/problem/D

给你一个有向图,dis[i][j]表示i到j的最短路,让你求dis[u][i] + dis[i][j] + dis[j][v]的最大值,其中u i j v互不相同。

先用优先队列的dijkstra预处理出i到j的最短距离(n^2 logn)。(spfa也可以做)

然后枚举4个点的中间两个点i j,然后枚举与i相连节点的最短路dis[u][i](只要枚举最长的3个就行了),接着枚举与j相连节点的最短路dis[j][v](同理也是枚举最长的3个就行了),复杂度为9*n^2。

 #include <bits/stdc++.h>
using namespace std;
const int MAXN = ;
typedef pair <int , int> P;
struct data {
int to , next;
}edge[MAXN];
int head[MAXN] , cnt , dis[MAXN][MAXN] , INF = 1e9;
vector <P> G[MAXN] , RG[MAXN]; inline void add(int u , int v) {
edge[cnt].next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
} bool judge(int x1 , int x2 , int x3 , int x4) {
if(x1 == x2 || x1 == x3 || x1 == x4 || x2 == x3 || x2 == x4 || x3 == x4)
return false;
return true;
} void dijkstra(int s) {
dis[s][s] = ;
priority_queue <P , vector<P> , greater<P> > que;
while(!que.empty()) {
que.pop();
}
que.push(P( , s));
while(!que.empty()) {
P temp = que.top();
que.pop();
int u = temp.second;
if(dis[s][u] < temp.first)
continue;
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(dis[s][v] > dis[s][u] + ) {
dis[s][v] = dis[s][u] + ;
que.push(P(dis[s][v] , v));
}
}
}
} int main()
{
memset(head , - , sizeof(head));
cnt = ;
int n , m , u , v;
scanf("%d %d" , &n , &m);
for(int i = ; i <= n ; i++) {
for(int j = ; j <= n ; j++) {
dis[i][j] = INF;
}
}
while(m--) {
scanf("%d %d" , &u , &v);
add(u , v);
}
for(int i = ; i <= n ; i++) {
dijkstra(i);
for(int j = ; j <= n ; j++) {
if(dis[i][j] != INF) {
G[i].push_back(P(dis[i][j] , j));
RG[j].push_back(P(dis[i][j] , i));
}
}
}
for(int i = ; i <= n ; i++) {
sort(G[i].begin() , G[i].end());
sort(RG[i].begin() , RG[i].end());
}
int r1 , r2 , r3 , r4 , Max = -;
for(int i = ; i <= n ; i++) {
for(int j = ; j <= n ; j++) {
if(i == j || dis[i][j] == INF)
continue;
for(int x = RG[i].size() - ; x >= max(int(RG[i].size() - ) , ) ; x--) {
for(int y = G[j].size() - ; y >= max(int(G[j].size() - ) , ) ; y--) {
int xx = RG[i][x].second , yy = G[j][y].second;
if(dis[i][j] + dis[xx][i] + dis[j][yy] >= Max && judge(i , j , xx , yy)) {
r1 = xx , r2 = i , r3 = j , r4 = yy;
Max = dis[i][j] + dis[xx][i] + dis[j][yy];
}
}
}
}
}
printf("%d %d %d %d\n" , r1 , r2 , r3 , r4);
}

Codeforces Round #349 (Div. 2) D. World Tour (最短路)的更多相关文章

  1. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  2. Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路

    B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...

  3. Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

    D. World Tour   A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...

  4. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  5. Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...

  6. Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)

    C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...

  7. Codeforces Round #349 (Div. 1)E. Forensic Examination

    题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少 题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s ...

  8. Codeforces Round #349 (Div. 2)

    第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...

  9. Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set

    C. Reberland Linguistics     First-rate specialists graduate from Berland State Institute of Peace a ...

随机推荐

  1. 发布 windows 10 universal app 时微软账号验证失败

    具体错误:Visual Studio encountered an unexpected network error and can't contact the Microsoft account s ...

  2. UVa 11427 (期望 DP) Expect the Expected

    设d(i, j)表示前i局每局获胜的比例均不超过p,且前i局共获胜j局的概率. d(i, j) = d(i-1, j) * (1-p) + d(i-1, j-1) * p 则只玩一天就就不再玩的概率Q ...

  3. UVALive 3661 Animal Run(最短路解最小割)

    题意:动物要逃跑,工作人员要截断从START(左上角)到END(右下角)的道路,每条边权表示拦截该条道路需要多少工作人员.问最少需要多少人才能完成拦截. 通俗地讲,就是把图一分为二所造成消耗的最小值. ...

  4. zollei的心动噪声探索性识别

    <Exploratory Identification of Cardiac Noise in fMRI Images> 这个方法,最特别的地方在于, 是从静息态的大脑数据中,抽取心动结构 ...

  5. poj 2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2858   Accepted: 1064 ...

  6. Struts2中通配符

    1.Struts2中通配符可通过请求的url路径来确定包.类.方法.返回值名. 如 <action name="*_*_*_*" class="cn.javass. ...

  7. Oracle 课程五之优化器和执行计划

    课程目标 完成本课程的学习后,您应该能够: •优化器的作用 •优化器的类型 •优化器的优化步骤 •扫描的基本类型 •表连接的执行计划 •其他运算方式的执行计划 •如何看执行计划顺序 •如何获取执行计划 ...

  8. codeforces 675C Money Transfers map

    上面是官方题解,写的很好,然后就A了,就是找到前缀和相等的最多区间,这样就可以减去更多的1 然后肯定很多人肯定很奇怪为什么从1开始数,其实从2开始也一样,因为是个环,从哪里开始记录前缀和都一样 我们的 ...

  9. DbContext运行时动态附加上一个dbset

    参考 Creating DbSet Properties Dynamically C# code? 1 DbSet<MyEntity> set = context.Set<MyEnt ...

  10. 基本输入输出系统BIOS---键盘输入

    基本输入输出系统BIOS概述 硬盘操作系统DOS建立在BIOS的基础上,通过BIOS操纵硬件,例如DOS调用BIOS显示I/O程序完成输入显示,调用打印I/O完成打印输出 通常应用程序应该调用DOS提 ...