题解 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算法就是模板算法了. 这里使 ...
随机推荐
- rz上传文件报错:rpm Read Signature failed: sigh blob(1268): BAD, read returned 0
上传文件报错: [root@www localdisk]# rpm -ivh cobbler* error: cobbler-2.8.4-4.el7.x86_64.rpm: rpm Read Si ...
- DHCP部署与安全
1.DHCP作用 (Dynamic Host Configure Protocol)自动分配ip地址 2.DHCP相关概念 地址池/作用域:(IP.子网掩码.网关.DNS.租期),DHCP协议端口是U ...
- 题解 guP4552 IncDec Sequence
这道题是一道差分的题目 差分数组p即p[i]=a[i]-a[i-1] 如果我们把一个区间[l,r]里的数+1,那么我们不难发现p[l]'=a[l]+1-a[l-1]=p[l]+1,p[r+1]'=a[ ...
- python删除文件中某一行
将文本中的 tasting123删除 with open("fileread.txt","r",encoding="utf-8") as f ...
- JAVA基础之JDK、JRE、JVM关系
什么是JRE和JDK JDK(Java Development Kit Java开发工具包) JDK是提供给Java开发人员使用的,其中包含了java的开发工具,也包括了JRE.所以安装了JDK,就不 ...
- Scala学习——函数
一.函数的定义(def) object FunctionApp { //定义函数:方法体内最后一行为返回值,不需要使用return def add(a:Int,b:Int):Int={ a + b } ...
- MySQL Orchestrator自动导换+VIP切换
目录 Orchestrator总体结构... 测试环境信息... Orchestrator详细配置... SSH免密配置... /etc/hosts配置... visudo配置... /e ...
- synchronized锁代码块(七)
synchronized同步代码块 用关键字synchronized声明方法在某些情况下是有弊端的,比如A线程调用同步方法执行一个较长时间的任务,那么B线程必须等待比较长的时间.这种情况下可以尝试使用 ...
- python 环境遇到的报错
pipenv install -r requirements.txt 时遇到错误: `Command "python setup.py egg_info" failed with ...
- flutter 解决无法安装或者安装依赖慢的问题
配置以下两个系统环境变量 右击计算机 --> 属性 --> 高级系统设置 --> 环境变量 PUB_HOSTED_URL : https://pub.flutter-io.cn FL ...