题目链接: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. 函数lock_rec_bitmap_reset

    /*********************************************************************//** Resets the record lock bi ...

  2. UVA 11090 Going in Cycle!!(二分答案+判负环)

    在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...

  3. [转载]ios入门篇 -hello Word(1)

    温馨提示:,如果您使用移动终端阅读本篇文章,请连接wifi的情况下阅读,里面有大量图片,以免造成您不必要的损失.   潜水博客园很多年,闲来无事,聊一下自己的经历,语文不好(如有什么错别字,请您在下评 ...

  4. Window 下 VFW 视频采集与显示

    引言 经过几天的努力终于将VFW视频采集与显示功能完整实现了,不得不说网上对这方面完整的详细讲解文章是在太少了.所以就要本人来好好总结一下让后来者不再像我一样折腾好久.在本文中我将详细讲解VFW视频采 ...

  5. 转载RabbitMQ入门(4)--路由

    路由 (使用Java客户端) 在先前的指南中,我们建立了一个简单的日志系统.我们可以将我们的日志信息广播到多个接收者. 在这部分的指南中,我们将要往其中添加一个功能-让仅仅订阅一个消息的子集成为可能. ...

  6. Delphi EVariantTypeCastError错误的解决方法

    在执行程序的时候总是提示: ---------------------------Debugger Exception Notification---------------------------P ...

  7. VS2010使用EventHandler发邮件

    转:http://blog.csdn.net/alfred_72/article/details/9980279 因为不知道VS2010 Sharepoint 有EventReciver这个添加项,走 ...

  8. Zend Framework 入门(2)—多国语言支持

    如果你的项目想要支持多语言版本,那么就需要用到 Zend_Translate.Zend_Translate 的详细文档在这里,不过如果想偷懒的话,也很简单,在View Helpers 文档中介绍了如何 ...

  9. win7和centos双系统安装

    几年之前为了安装xp和linux的双系统曾折腾了好多天,今天为了安装这个win7和centos双系统,也折腾了两天多,哦,我的天,安装个双系统,怎么这么麻烦呢? 没有来得及整理,先铺上草稿,供同志们参 ...

  10. UI篇--android实现底部按钮布局

    1.采用LinearLayout布局: <LinearLayout android:id="@+id/main" android:layout_width="fil ...