题解 graph
一道做了巨久,不过确实很好的题
发现不定边权极难处理,所以就不会
感觉和这题有点像,但还是不会
但发现题面里有个地方很套路
所以考虑如何维护凸包
根据题解,发现为了确定一条路径的权值,我们需要知道这条路上经过了多少条-1边
所以需要处理出经过 \(k\) 条-1边时到终点的最短路
发现这个东西很难实现,考虑二维spfa
- 当题目要求「在某种特定访问顺序(先去过红点才能去蓝点/必须按一红一蓝访问之类)/特定前提/访问分层」条件下的最短路时,考虑二维最短路(其实就是开个二维数组记录下当前限制条件满足到什么情况了
然后就可以用凸包维护有哪些k可能形成最优值
这里有个细节,我一直写的是计算 \(top-1\) 和 \(top\) 的交点,再计算 \(top\) 和当前 \(i\) 的交点横坐标判断
但实际上好像应该是计算 \(top-1\) 和当前 \(i\) 的交点及 \(top\) 和当前 \(i\) 的交点
若两交点横坐标间没有整数值就可以弹掉了
仔细想想或画个图会发现,我们令 \(top-1\) 和 \(top\) 的交点为 \(x,top\) 和当前 \(i\) 的交点为y
那我们截取 \(xy\) 这条线段,发现这条线段实际上属于当前 \(i\) 而不是 \(top\)
所以就清楚了
于是我们在spfa时记录路径,dfs回溯标记下都经过了哪些点输出即可
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 1010
#define ll long long
#define int long long
#define fir first
#define sec second
#define make make_pair
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n, m;
int head[N], size, dis[2010][N], top;
vector< pair<int, int> > back[2010][N];
bool vis[2010][N], ans[N], vis2[N];
struct edge{int to, next, val;}e[5010];
inline void add(int s, int t, int w) {edge* k=&e[++size]; k->to=t; k->val=w; k->next=head[s]; head[s]=size;}
struct que{double k, b; inline void build(int k_, int b_) {k=k_; b=b_;} que(){} que(int k_, int b_):k(k_),b(b_){}}q[N];
inline double point(que a, que b) {return (a.b-b.b)/(b.k-a.k);}
void spfa(int s) {
memset(dis, 127, sizeof(dis));
queue< pair<int, int> > q;
pair<int, int> t;
dis[0][s]=0;
q.push(make(0, 1));
while (q.size()) {
t=q.front(); q.pop();
vis[t.fir][t.sec]=0;
if (t.fir>m) continue;
//cout<<"t: "<<t.fir<<' '<<t.sec<<endl;
for (int i=head[t.sec],v; i; i=e[i].next) {
v = e[i].to;
if (e[i].val==-1) {
if (dis[t.fir+1][v] > dis[t.fir][t.sec]) {
dis[t.fir+1][v]=dis[t.fir][t.sec];
back[t.fir+1][v].clear();
back[t.fir+1][v].push_back(make(t.sec, -1));
if (!vis[t.fir+1][v]) q.push(make(t.fir+1, v)), vis[t.fir+1][v]=1;
}
else if (dis[t.fir+1][v] == dis[t.fir][t.sec]) back[t.fir+1][v].push_back(make(t.sec, -1));
}
else {
if (dis[t.fir][v] > dis[t.fir][t.sec]+e[i].val) {
dis[t.fir][v]=dis[t.fir][t.sec]+e[i].val;
back[t.fir][v].clear();
back[t.fir][v].push_back(make(t.sec, 0));
if (!vis[t.fir][v]) q.push(make(t.fir, v)), vis[t.fir][v]=1;
}
else if (dis[t.fir][v] == dis[t.fir][t.sec]+e[i].val) back[t.fir][v].push_back(make(t.sec, 0));
}
}
}
//cout<<"dis: "; for (int i=0; i<=10; ++i) {for (int j=1; j<=n; ++j) cout<<dis[i][j]<<' '; cout<<endl;}
}
void dfs(int u, int k) {
//cout<<"dfs "<<u<<' '<<k<<endl;
//for (int i=1; i<=n; ++i) printf("%d", ans[i]); printf("\n");
if (!u) return ;
if (!k && u==1) {ans[1]=1; return ;}
ans[u]=1; vis[k][u]=1;
for (auto it:back[k][u]) if (!vis[k+it.sec][it.fir]) dfs(it.fir, k+it.sec);
}
void dfs2(int u) {
//cout<<"dfs2 "<<u<<endl;
ans[u]=1; vis2[u]=1;
for (int i=head[u]; i; i=e[i].next)
if (e[i].val==-1 && !vis2[e[i].to]) dfs2(e[i].to);
}
signed main()
{
n=read(); m=read();
for (int i=1,u,v,w; i<=m; ++i) {
u=read(); v=read(); w=read();
add(u, v, w); add(v, u, w);
}
spfa(1);
for (int i=0; i<=m; ++i) {
//cout<<"try "<<i<<' '<<dis[i][n]<<' '<<q[top].k<<' '<<q[top].b<<endl;
if (top && q[top].b<dis[i][n]) continue;
//if (top) cout<<"now goto while: "<<ceil(point(q[top-1], q[top]))<<' '<<floor(point(q[top], que(i, dis[i][n])))<<endl;
while (top>1 && ceil(point(q[top], que(i, dis[i][n])))>floor(point(q[top-1], que(i, dis[i][n])))) {
//cout<<ceil(point(q[top-1], q[top]))<<' '<<point(q[top], que(i, dis[i][n]))<<endl;
//cout<<"pop"<<endl;
--top;
}
q[++top].build(i, dis[i][n]);
}
//cout<<"top: "<<top<<' '<<q[top].k<<' '<<q[top].b<<endl;
for (int i=1; i<=top; ++i) dfs(n, q[i].k);
//for (int i=1; i<=n; ++i) if (ans[i]) dfs2(i);
dfs2(1); dfs2(n);
for (int i=1; i<=n; ++i) printf("%lld", ans[i]);
printf("\n");
return 0;
}
题解 graph的更多相关文章
- [LeetCode]题解(python):133-Clone Graph
题目来源: https://leetcode.com/problems/clone-graph/ 题意分析: 克隆一个无向图.每个节点包括一个数值和它的邻居. 题目思路: 直接深度拷贝. 代码(pyt ...
- Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学
Sasha and Interesting Fact from Graph Theory n 个 点形成 m 个有标号森林的方案数为 F(n, m) = m * n ^ {n - 1 - m} 然后就 ...
- Hdoj 2454.Degree Sequence of Graph G 题解
Problem Description Wang Haiyang is a strong and optimistic Chinese youngster. Although born and bro ...
- POJ 2553 The Bottom of Graph 强连通图题解
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- POJ 1737 Connected Graph 题解(未完成)
Connected Graph Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3156 Accepted: 1533 D ...
- 题解 CF915D 【Almost Acyclic Graph】
这道题我第一次的想法是直接判环的数量,然而事实证明实在是太naive了. 随便画个图都可以卡掉我的解法.(不知道在想什么) 这道题的正解是拓扑排序. 朴素的想法是对所有边都跑一次拓扑,但这样$O(m( ...
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- 【题解】G.Graph(2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest)
题目链接G题 题意 序列 \(a_1,a_2,⋯,a_n\) 是一个排列, 当且仅当它含有 1 到 n 的所有整数. 排列 \(a_1,a_2,⋯,a_n\) 是一个有向图的拓扑排序,当且仅当对于每条 ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
随机推荐
- MindSpore模型精度调优实战:常用的定位精度调试调优思路
摘要:在模型的开发过程中,精度达不到预期常常让人头疼.为了帮助用户解决模型调试调优的问题,我们为MindSpore量身定做了可视化调试调优组件:MindInsight. 本文分享自华为云社区<技 ...
- Oracle如何以逗号分隔的字符串拆分为多行数据
近期在工作中遇到某表某字段是可扩展数据内容,信息以逗号分隔生成的,现需求要根据此字段数据在其它表查询相关的内容展现出来,第一想法是切割数据,以逗号作为切割符,以下为总结的实现方法,以供大家参考.指教. ...
- 比较app版本大小----python
def compare(a: str, b: str): '''比较两个版本的大小,需要按.分割后比较各个部分的大小''' lena = len(a.split('.')) # 获取版本字符串的组成部 ...
- NIO 输入输出
NIO 是java14 API 提供的一种新输入输出流,一套用于标准IO的文件读写,一套用于网络编程. 1. NIO 与IO 的区别 IO流以字节流输入输出,一次以一个字节进行数据操作,效率慢: NI ...
- Python单元测试框架unittest之批量用例管理(discover)
前言 我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到unittet里面的discover方法来加载用例了.加载用例后,用unittest里面的TextT ...
- java面向对象程序设计(下)-枚举类
在某些情况下,一个类的对象是有限而且固定的,比如季节类,它只有4个对象;再比如行星类,目前只有8个对象,这些实例有限而且固定的类,在Java中被称为枚举类 JDK1.5新增了一个enum关键字,(它与 ...
- 答读者问(1):非模式物种找marker;如何根据marker定义细胞类型
下午花了两个小时回答读者的疑问,觉得可以记录下来,也许能帮到一部分人. 第一位读者做的是非模式物种的单细胞. 一开始以为是想问我非模式物种的marker基因在哪儿找,读者朋友也提到了blast 研究的 ...
- 【搜索】棋盘 luogu-3956
分析 按照这个题目随便写一个搜索就可以了 AC代码 #include <cstdio> #include <cstring> #include <algorithm> ...
- Linux 各个命令的缩写原型
cd:(change derictery)更换目录: pwd:(Print Working Directory)显示当前工作目录: mk:(make directory)创建目录: ...
- python -- 负数平方根-虚数的使用
负数的平方根是虚数. 不能使用sqrt,因为它只能处理浮点数,而虚数是完全不同的--这也是由另外一个叫做cmath(即 complex math, 复数)的模块来实现这些功能的原因. >> ...