题目链接: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. 百度HTTPS加密搜索有什么用?

    前段时间,我曾提到百度支持移动端HTTPS SSL加密搜索,用以保护用户隐私.最近,百度开始支持PC端HTTPS SSL加密搜索,现在可以启用 https://www.baidu.com 搜索.我很少 ...

  2. Java 比较两张图片的相似度

    import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; /** * 比较两张图片 ...

  3. Spring 使用注解方式进行事务管理

    转载:http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html 使用步骤: 步骤一.在spring配置文件中引入<tx:&g ...

  4. fancybox 无效 失效 直接打开页面, ajax 之后 fancybox对更新的数据无效,Jquery失效 无效

    案例:做个聊天室项目,数据都是通过ajax刷新出来的,而对新数据绑定的fancybox均无效,点击直接打开到了新页面而不是弹窗,解决方法其实很简单   简单分析:ajax加载内容是在$(documen ...

  5. BZOJ 1861 书架

    (╯-_-)╯╧╧ 此处为错误代码. #include<iostream> #include<cstdio> #include<cstring> #include& ...

  6. JavaScript的事件代理(转)

    如果你想给网页添加点JavaScript的交互性,也许你已经听过JavaScript的事件代理(event delegation),并且觉得这是那些发烧友级别的JavaScript程序员才会关心的什么 ...

  7. HDU 5818 Joint Stacks

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  8. sql server 2012序列号

    MICROSOFT SQL SERVER 2012 企业核心版激活码序列号: FH666-Y346V-7XFQ3-V69JM-RHW28 MICROSOFT SQL SERVER 2012 商业智能版 ...

  9. erp验收测试

    软件测试是为了发现错误而执行程序的过程.它不仅是软件开发阶段的有机组成部分,而且在整个软件工程(即软件定义.设计和开发过程)中占据相当大的比重.软件测试是软件质量保证的关键环节,直接影响着软件的质量评 ...

  10. ORA-15177: cannot operate on system aliases (DBD ERROR: OCIStmtExecute)

    ASM操作的时候,删除一个文件夹,删除不了,报错如下: ASMCMD> ls -l Type Redund Striped Time Sys Name Y IPAP/ ASMCMD> rm ...