题解 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算法就是模板算法了. 这里使 ...
随机推荐
- Java | Random 和 Math 的概述及使用
Random Random类是java.util的包里面提供的我们常用的API,方便我们操作的,还有非常多像Random一样的类. Random的作用 生成一个随机数字,可以指定范围,也可以真的随机. ...
- s3cmd的使用
目录 1. 安装s3cmd 2. 配置s3cmd 3. 使用s3cmd [前言] s3cmd 是用于创建S3桶,上传,检索和管理数据到对象存储命令行实用程序. 本文将指导linux下安装s3cmd程序 ...
- mac-webui-selenium下的webdriver selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH
from selenium import webdriver def test1(): url='http://www.baidu.com' driver=webdriver.Chrome(" ...
- python找出字典中value最大值的几种方法
假设定义一字典,m = {"a":3,"e":6,"b":2,"g":7,"f":7,"c ...
- "Shortest" pair of paths[题解]
"Shortest" pair of paths 题目大意 给出 \(n\) 个点,\(m\) 条边,除第一个点和最后一个点外,其他所有的点都只能被经过一次,要求找到两条从第一个点 ...
- C语言:case详解
C语言虽然没有限制 if else 能够处理的分支数量,但当分支过多时,用 if else 处理会不太方便,而且容易出现 if else 配对出错的情况.例如,输入一个整数,输出该整数对应的星期几的英 ...
- LeetCode 778. Swim in Rising Water
题目链接:https://leetcode.com/problems/swim-in-rising-water/ 题意:已知一个n*n的网格,初始时的位置为(0,0),目标位置为(n-1,n-1),且 ...
- 微信小程序云开发-云存储的应用-识别驾驶证
一.准备工作 1.创建云函数identify 2.云函数identify中index.js代码 1 // 云函数入口文件 2 const cloud = require('wx-server-sdk' ...
- 2个Double字符串进行
public static int compare(double d1, double d2) { if (d1 < d2) return -1; // Neither val is NaN, ...
- 第三篇--如何修改exe文件版本号和文件信息
控制台程序添加版本信息方法: 项目右键 Add-->Resource-->选择Version-->new,然后就可以修改里面的信息了,重新编译一下就OK.