[SDOI 2009]Elaxia的路线
Description
Input
Output
Sample Input
1 6 7 8
1 2 1
2 5 2
2 3 3
3 4 2
3 9 5
4 5 3
4 6 4
4 7 2
5 8 1
7 9 1
Sample Output
HINT
对于30%的数据,N ≤ 100;
对于60%的数据,N ≤ 1000;
对于100%的数据,N ≤ 1500,输入数据保证没有重边和自环。
题解
一开始看点数小,直接强行$SPFA$预处理出第一条路径,然后再$SPFA$第二条路径时记录答案。$TLE$...
首先,分别从$x_1$,$y_1$,$x_2$,$y_2$为源点分别跑一次$SPFA$,记$dis[0][u]$,$dis[1][u]$,$dis[2][u]$,$dis[3][u]$分别为从$x_1$,$y_1$,$x_2$,$y_2$为起始点到点$u$的最短路径。
构建出一个新的有向图,包含$x_1->y_1$和$x_2->y_2$的所有最短路上的边。判断一条边$u->v$是否在$x_1->y_1$的最短路上,就是判断$dis[1][u]+val(u,v)+dis[2][v]==dis[1][y1]$($val(u,v)$为边$u->v$的长度),如果为真,那么在最短路上,为假则不在最短路上。是否在$x_2->y_2$的最短路上同理。
不过要注意一点,因为边的走向可能是$u->v$,也可能是$v->u$。所以,对于一条边必须判断两次(第一次为$u->v$,第二次为$v->u$)。
- 最后,按照新图的拓扑序,在各个点之间进行递推,求得答案。
//It is made by Awson on 2017.10.20
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define y1 yy
using namespace std;
const int N = ; int n, m, x1, x2, y1, y2, u, v, c;
struct tt {
int to, cost, next;
bool color;
}edge[(N*N>>)+], g[(N*N>>)+];
int path[N+], top, path_g[N+], top_g;
int dist[][N+], ans, cnt[N+], in[N+];
struct node {
int x, val;
node (){
}
node (int _x, int _val) {
x = _x, val = _val;
}
}; void init(int u, int v, int c) {
g[++top_g].to = v;
g[top_g].cost = c;
g[top_g].next = path_g[u];
path_g[u] = top_g;
}
void add(int u, int v, int c) {
edge[++top].to = v;
edge[top].next = path[u];
edge[top].cost = c;
path[u] = top;
}
void topsort() {
queue<node>Q; while (!Q.empty()) Q.pop();
for (int i = ; i <= n; i++) if (!in[i]) Q.push(node(i, ));
while (!Q.empty()) {
int u = Q.front().x, val = Q.front().val; Q.pop();
ans = Max(ans, val);
for (int i = path_g[u]; i; i = g[i].next) {
int v = g[i].to; in[v]--; cnt[v] = Max(cnt[v], val+g[i].cost);
if (!in[v]) Q.push(node(v, cnt[v]));
}
}
}
void SPFA(int x, int t) {
bool vis[N+] = {}; vis[x] = ;
memset(dist[t], /, sizeof(dist[t])); dist[t][x] = ;
queue<int>Q; while (!Q.empty()) Q.pop(); Q.push(x);
while (!Q.empty()) {
int u = Q.front(); Q.pop(); vis[u] = ;
for (int i = path[u]; i; i = edge[i].next) {
int v = edge[i].to;
if (dist[t][v] > dist[t][u]+edge[i].cost) {
dist[t][v] = dist[t][u]+edge[i].cost;
if (!vis[v]) {
vis[v] = ; Q.push(v);
}
}
}
}
}
void work() {
scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2);
for (int i = ; i <= m; i++) {
scanf("%d%d%d", &u, &v, &c);
add(u, v, c), add(v, u, c);
}
SPFA(x1, ); SPFA(y1, ); SPFA(x2, ), SPFA(y2, );
for (int u = ; u <= n; u++)
for (int i = path[u]; i; i = edge[i].next) {
int v = edge[i].to;
if (dist[][u]+dist[][v]+edge[i].cost != dist[][y1]) continue;
if (dist[][u]+dist[][v]+edge[i].cost != dist[][y2]) continue;
init(u, v, edge[i].cost); in[v]++;
}
topsort();
memset(path_g, , sizeof(path_g)); top_g = ;
memset(cnt, , sizeof(cnt));
for (int u = ; u <= n; u++)
for (int i = path[u]; i; i = edge[i].next) {
int v = edge[i].to;
if (dist[][u]+dist[][v]+edge[i].cost != dist[][y1]) continue;
if (dist[][u]+dist[][v]+edge[i].cost != dist[][y2]) continue;
init(u, v, edge[i].cost); in[v]++;
}
topsort();
printf("%d\n", ans);
}
int main() {
work();
return ;
}
[SDOI 2009]Elaxia的路线的更多相关文章
- 「SDOI 2009」Elaxia的路线
发现自己这几天智商完全不在线-- 这道题的数据十分的水,怎样都可以艹过去-- 开始想了一个完全错误的算法,枚举一对点,判断这一对点是否同时在两条最短路上,是就用两点之间的路径更新答案.显然这样是错的: ...
- BZOJ-1880 Elaxia的路线 SPFA+枚举
1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 921 Solved: 354 [Submit][Sta ...
- BZOJ 1880: [Sdoi2009]Elaxia的路线( 最短路 + dp )
找出同时在他们最短路上的边(dijkstra + dfs), 组成新图, 新图DAG的最长路就是答案...因为两人走同一条路但是不同方向也可以, 所以要把一种一个的s,t换一下再更新一次答案 ---- ...
- 【BZOJ1880】[Sdoi2009]Elaxia的路线(最短路)
[BZOJ1880][Sdoi2009]Elaxia的路线(最短路) 题面 BZOJ 洛谷 题解 假装我们知道了任意两点间的最短路,那么我们怎么求解答案呢? 不难发现公共路径一定是一段连续的路径(如果 ...
- 洛谷 P2149 [SDOI2009]Elaxia的路线 解题报告
P2149 [SDOI2009]Elaxia的路线 题目描述 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间. Elaxia ...
- [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)
[BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...
- P2149 Elaxia的路线
P2149 Elaxia的路线 题意简述: 在一个n(n<=1500)个点的无向图里找两对点之间的最短路径的最长重合部分,即在保证最短路的情况下两条路径的最长重合长度(最短路不为一) 思路: 两 ...
- 【BZOJ 1880】 [Sdoi2009]Elaxia的路线 (最短路树)
1880: [Sdoi2009]Elaxia的路线 Description 最近,Elaxia和w**的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间. ...
- BZOJ1880: [Sdoi2009]Elaxia的路线(最短路)
1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 2049 Solved: 805 题目链接:https ...
随机推荐
- C 连接mysql VC的步骤
初学C,看到C 连接mysql的教程不是很多,遇到很多的问题,看过许多盟友的解决方法,有点模糊(对我这个菜鸟来说),下面贴出具体步骤,一起学习: 1.C连接mysql的方法:C ,C ++ ,ODBC ...
- 听翁恺老师mooc笔记(6)--指针运算
指针值加1就是将指针值加上sizeof(指针所指变量的类型) 1+1=2,那么指针加1是加上了1这个数字吗?试一下,在代码中定义了char数组,char也是整数,数组名是ac,ac中有10个元素,0- ...
- 【Alpha版本】冲刺阶段 - Day4 - 加速
今日进展 袁逸灏:实现音乐的播放.(5h) 启动类,游戏画面类(修改类) 刘伟康:继续借鉴其他 alpha 冲刺博客,了解了Android方面的部分内容,便于更好地推进进度.(2h) 刘先润:更新图画 ...
- 敏捷开发每日报告--day5
1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285) Git链接:https://github.com/WHUSE2017/C-team 2 ...
- Week03-面向对象入门
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 类 对象 封装 继承 覆盖 重载 构造函数 static public private toString f ...
- iOS极光推送SDK的使用流程
一.极光推送简介 极光推送是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,整合了iOS.Android和WP平台的统一推送服务.使用起来方便简单,已于集成,解决了原生远程推送繁 ...
- Flask 学习 十 博客文章
提交和显示博客文章 app/models.py 文章模型 class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer ...
- JAVA和Android的回调机制
本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢 以 前不理解什么叫回 ...
- Spring Cache扩展:注解失效时间+主动刷新缓存(二)
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- ( 转 ) 聊一聊C#的Equals()和GetHashCode()方法
聊一聊C#的Equals()和GetHashCode()方法 博客创建一年多,还是第一次写博文,有什么不对的地方还请多多指教. 关于这次写的内容可以说是老生长谈,百度一搜一大堆.大神可自行绕路. ...