传送门

一道做了巨久,不过确实很好的题

发现不定边权极难处理,所以就不会

感觉和这题有点像,但还是不会

但发现题面里有个地方很套路

  • 要求有哪些点/边最终可以满足最短/最小,比如这样这样的题,考虑凸包,最终在凸包上的点就是能取到最值的点

所以考虑如何维护凸包

根据题解,发现为了确定一条路径的权值,我们需要知道这条路上经过了多少条-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的更多相关文章

  1. [LeetCode]题解(python):133-Clone Graph

    题目来源: https://leetcode.com/problems/clone-graph/ 题意分析: 克隆一个无向图.每个节点包括一个数值和它的邻居. 题目思路: 直接深度拷贝. 代码(pyt ...

  2. 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} 然后就 ...

  3. Hdoj 2454.Degree Sequence of Graph G 题解

    Problem Description Wang Haiyang is a strong and optimistic Chinese youngster. Although born and bro ...

  4. POJ 2553 The Bottom of Graph 强连通图题解

    Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...

  5. POJ 1737 Connected Graph 题解(未完成)

    Connected Graph Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3156   Accepted: 1533 D ...

  6. 题解 CF915D 【Almost Acyclic Graph】

    这道题我第一次的想法是直接判环的数量,然而事实证明实在是太naive了. 随便画个图都可以卡掉我的解法.(不知道在想什么) 这道题的正解是拓扑排序. 朴素的想法是对所有边都跑一次拓扑,但这样$O(m( ...

  7. POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)

    Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...

  8. 【题解】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\) 是一个有向图的拓扑排序,当且仅当对于每条 ...

  9. POJ 2553 The Bottom of a Graph TarJan算法题解

    本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...

随机推荐

  1. wumei-smart智能家居开原项目

    一.项目简介 物美智能(wumei-smart)]是一套开源的软硬件系统,可用于二次开发和学习,快速搭建自己的智能家居系统. 硬件工程师可以把自己的设备集成到系统:软件工程师可以使用项目中的设备熟悉软 ...

  2. 一次性讲清楚spring中bean的生命周期之三:bean是如何实例化的

    在前面的两篇博文<一次性讲清楚spring中bean的生命周期之一:getSingleton方法>和<一次性讲清楚spring中bean的生命周期之二:FactoryBean的前世今 ...

  3. python pycharm 正则表达式批量替换

    {accept:application/json, text/plain, */*,accept-encoding:gzip, deflate, br,accept-language:zh-CN,zh ...

  4. c++中的静态成员

    引言 有时候需要类的一些成员与类本身相关联,而不是与类的每个对象相关联.比如类的所有对象都要共享的变量,这个时候我们就要用到类的静态成员. 声明类的静态成员 声明静态成员的方法是使用static关键字 ...

  5. 【经典结构】单例模式Singleton

    单例模式Singleton 1.含义 单例模式:即一个类只能创建一个实例. 只有一个实例 --> 不可以从类外new对象 --> 构造器私有化private --> 从类里创建实例: ...

  6. React组件三大属性之 props

    React组件三大属性之 props 理解1) 每个组件对象都会有props(properties的简写)属性2) 组件标签的所有属性都保存在props中 作用1) 通过标签属性从组件外向组件内传递变 ...

  7. java并发编程基础——线程通信

    线程通信 当线程在系统内运行时,程序通常无法准确的控制线程的轮换执行,但我们可以通过一些机制来保障线程的协调运行 一.传统的线程通信 传统的线程通信主要是通过Object类提供的wait(),noti ...

  8. springboot-3-web开发

    一.视图层技术thymeleaf 我们一般都是基于3.x版本 1.流程: 导入依赖 <!--整合thymeleaf技术--> <dependency> <groupId& ...

  9. OpenFaaS实战之二:函数入门

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  10. odoo源码学习之任务中的阶段字段stage_id

    # 案例0004针对form表单 class Task(models.Model): _name = "project.task" _description = "对于项 ...