题解 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算法就是模板算法了. 这里使 ...
随机推荐
- Arduino IDE 2.0 beta安装
1.在官网(Software | Arduino)下载安装包,此次提供操作系统有:Windows.Linux和macOC系统 2.点击安装包进行安装 3.点击我同意 4.点击下一步 5.选择安装路径( ...
- 「AGC010F」 Tree Game
「AGC010F」 Tree Game 传送门 切了一个 AGC 的题,很有精神. 于是决定纪念一下. 首先如果任意一个人在点 \(u\),他肯定不会向点权大于等于 \(a_u\) 的点走的,因为此时 ...
- Leetcode春季活动打卡第三天:面试题 10.01. 合并排序的数组
Leetcode春季活动打卡第三天:面试题 10.01. 合并排序的数组 Leetcode春季活动打卡第三天:面试题 10.01. 合并排序的数组 思路 这道题,两个数组原本就有序.于是我们采用双指针 ...
- LCT(Link-Cut-Tree)
LCT(Link-Cut-Tree) LCT维护一个森林,即把每个节点用splay维护,可以进行许多操作: 查询.修改链上的信息 随意指定原树的根(即换根) 动态连边.删边 合并两棵树.分离一棵树 动 ...
- Bootstrap框架--DataTables列表示例--添加判断
一.参考代码 <%@ include file="./include/header.jsp"%> <!-- jquery.dataTables.css --> ...
- c语言学习篇二【基础语法】
一.定义常量: 使用 #define 预处理器. 使用 const 关键字. #include <stdio.h> int main() { const int LENGTH = 10;/ ...
- sql注入之类型及提交注入
#参数类型 这里说的参数是源码中存在注入的地方. 其中参数类型有:数字.字符.搜索.json等. 其中sql语句干扰符号有:',",%,),}等,过滤首先考虑闭合这些符号,再进行注入测试. ...
- 科普—为什么要用ECDSA加签及其数学上的验签证明
在上文介绍了ECDSA算法流程及模块划分,为了帮助一些小白弄懂啥是ECDSA,特此开一篇科普博文. 一.首先为啥要进行数字签名? 假设Alice要将一份合同m传输给Bob,合同上附有Alice的电子纸 ...
- 获取浏览器中url的参数
例如: 浏览器的地址是:http://localhost:8080/src/views/moneyDetail?id=10 vue 获取浏览器的参数 获取id的参数:this.$route.query ...
- C++ //函数调用运算符重载 (仿函数)
1 //函数调用运算符重载 2 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 //函 ...