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 ...
随机推荐
- android动画坐标定义
这段时间一直在忙Android的项目,总算抽出点时间休息一下,准备把一些项目用到的Android经验分享一下. 在Android开发过程中,经常会碰到Activity之间的切换效果的问题,下面介绍一下 ...
- 基于XMPP的即时通信系统的建立(二)— XMPP详解
XMPP详解 XMPP(eXtensible Messaging and Presence Protocol,可扩展消息处理和现场协议)是一种在两个地点间传递小型结构化数据的协议.在此基础上,XMPP ...
- Spring 使用注解方式进行事务管理
转载:http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html 使用步骤: 步骤一.在spring配置文件中引入<tx:&g ...
- UVa 11529 (计数) Strange Tax Calculation
枚举一个中心点,然后将其他点绕着这个点按照极角排序. 统计这个中心点在外面的三角形的个数,然后用C(n-1, 3)减去这个数就是包含这个点的三角形的数量. 然后再枚举一个起点L,终点为弧度小于π的点R ...
- CodeForces Round #278 (Div.2) (待续)
A 这么简单的题直接贴代码好了. #include <cstdio> #include <cmath> using namespace std; bool islucky(in ...
- Java知识点:琐碎知识点(1)
Java基本介绍 SUN:Stanford University NetworkJava之父:James GoslingJava的跨平台性因为有Java虚拟机,运行class文件.Java吉祥物:Du ...
- OK335xS psplash make-image-header.sh hacking
/***************************************************************************** * OK335xS psplash mak ...
- UVA 10801 Lift Hopping 电梯换乘(最短路,变形)
题意: 有n<6部电梯,给出每部电梯可以停的一些特定的楼层,要求从0层到达第k层出来,每次换乘需要60秒,每部电梯经过每层所耗时不同,具体按 层数*电梯速度 来算.问经过多少秒到达k层(k可以为 ...
- Liunx系统学习一,liunx系统的目录结构及含义
LIUNX系统目录结构: “/” ===>这是linux文件系统的入口,也是整个linux文件系统的根目录,linux不同于windows,没有所谓的C,D,E盘,整个liunx只有一个根分区 ...
- js除法余数
return (Math.round(rs*100)/100); //保保留小数点后两位数: //如果要保留三位则改为:Math.round(rs*1000)/1000; //如果要保留四位则改为:M ...