Codeforces Round #349 (Div. 2) D. World Tour (最短路)
题目链接: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 (最短路)的更多相关文章
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...
- 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 ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划
A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp
题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...
- 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 ...
- Codeforces Round #349 (Div. 1)E. Forensic Examination
题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少 题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s ...
- Codeforces Round #349 (Div. 2)
第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set
C. Reberland Linguistics First-rate specialists graduate from Berland State Institute of Peace a ...
随机推荐
- Struts2系列——struts2的result
在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作. 在ActionSupport基类中定 ...
- JS省队集训记
不知不觉省队集训已经结束,离noi也越来越近了呢 论考前实战训练的重要性,让我随便总结一下这几天的考试 Day 1 T1 唉,感觉跟xj测试很像啊?meet in middle,不过这种题不多测是什么 ...
- bzoj2878
又是环套树dp,这次不是我擅长的类型 首先考虑树上的暴力,肯定是穷举起点然后以起点为根dp 我们用g[i]表示以点i为期望走的路径总长,答案就是1/n*Σ(g[i]/d[i]) (d[i]表示点度数) ...
- 64位SqlServer通过链接服务器与32位oracle通讯
在SQL SERVER里只安装了32位的oracle客户端的情况下,添加链接服务器然后执行查询会报如下信息: 原因:在64位的SQL Engine中已经不提供MSDAORA 的驱动了,可以使用Ora ...
- UVA 10801 Lift Hopping 电梯换乘(最短路,变形)
题意: 有n<6部电梯,给出每部电梯可以停的一些特定的楼层,要求从0层到达第k层出来,每次换乘需要60秒,每部电梯经过每层所耗时不同,具体按 层数*电梯速度 来算.问经过多少秒到达k层(k可以为 ...
- jQuery live与bind的区别
平时在使用jQuery进行AJAX操作的时候,新生成的元素事件会失效,有时候不得不重新绑定一下事件,但是这样做很麻烦.例如评论分页后对评论内容的JS验证会失效等.在jQuery1.3之前有一个插件会解 ...
- 【C#学习笔记】读access2007
using System; using System.Data.OleDb; namespace ConsoleApplication { class Program { static void Ma ...
- poj 3211 Washing Clothes
// 题意 :夫妻两洗衣服,衣服有m种颜色,每种颜色又有若干件,每件衣服洗完需要特定的时间,要求每种颜色放在一起洗,洗完才能洗其他衣服.最后问洗完需要的最少时间// 将衣服按颜色分类 然后求出每种颜色 ...
- Android-取消GridView/ListView item被点击时的效果
方法一,在控件被初始化的时候设置 gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); listView.setSelector(ne ...
- Linux下的sniffer工具--TcpDump的安装和使用
在如今众多的黑客技术中,嗅探器(sniffer)是最常见,也是最重要的技术之一. 用过windows平台上的sniffer工具(例如,netxray和sniffer pro软件)的朋友可能都知道,在共 ...