Graph & Tree2
续https://www.cnblogs.com/tyqtyq/p/9769817.html
0x65
负环
- SPFA
- 当一个节点入队次数到达N的时候,就说明有负环
- 或者记录最短路包含的路径条数
- 还有其他优化手段,如将SPFA的队列换成栈,使用dfs等等
差分约束系统
- 特殊的不等式组,有N个不等式
- 任意不等式都是形如这样的:\(X_{i} - X_{j} \leq c_{k}\)
- 我们就可以建一张图使得对于任意不等式\(X_{i} - X_{j} \leq c_{k}\),在i,j之间连一条长度为\(c{k}\)的边
- 那么我们求解单源最短路,\(X_{i} = dist_{i}\)就是一组解
- 但是为了保证图的联通性,应该加一个虚拟节点\(X_{0}\)使得其到所有节点距离为0,从\(X_{0}\)开始跑SPFA就可以求出解了
- 如果有负环则无解
0x66
卡了我几天的tarjan算法QAQ
tarjantql!
- 深搜一遍求出时间戳
- 定义一个追溯值\(low_{x}\)为\(min{\{i|i \in \{ {a|a \in subtree(x) || a \in } }{a可通过一条不在搜索树上的边到达subtree(x)中的节点\} \}}\)
- 那么珂以通过一遍dfs搜出dfn和low数组
- 接下来讲讲如何判定是否是割点/边:
割边判定:边(x,y)是割边当且仅当\(dfn_{x} \le low_{y}\)
- 直接根据定义yy一下即可
- 无向图需要处理一下重边个数问题:
- 当fa为x的父亲的时候,如果有重边则珂以用来更新
- 珂以用成对变换解决
割点判定:
- 对于不是搜索树根节点点x,若存在一x的子节点y使得\(dfn_{x} \leq low_{y}\),则x是割点
- 如x为根节点,则需要两个这样的y
- 割点为\(\leq\),不需考虑父节点与重边
缩点
- 定义:若一个无向图没有割点,则称其为点双联通图,同理我们称没有割边的图为边双联通图
- 无向图的极大双联通子图被称为点双联通分量,边同理
我trl跳过无向图tarjan缩点
好吧我图论渣渣就是没办法,放了放了,先搞Dp和数据结构,以后再来填这个坑
I'm BACK!
JZOJ图论好题:
3936. 【GDOI2015模拟11.22】假期计划
观察到k很小,珂以对所有k执行dijkstra求出最短路,时间复杂度O(KNlgN)
对于一次询问(x,y),先暴力枚举一遍所有枢纽,设枚举到枢纽i,计算(x->i->y)的最短路,珂以O(1)求出
时间复杂度O(KNlgN+QM),卡卡常就珂以通过此题
/*
Copyright(c) TYQ
All rights served;
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#define MAXN 20005
#define MAXM 20005
#define MAXK 205
#define MAXQ 50005
using namespace std;
int edge[MAXM],ver[MAXM],Next[MAXM],head[MAXN],temp[MAXK] ;
int d[MAXK][MAXN];
bool v[MAXQ],intemp[MAXN] ;
int N,K,M,Query,tot,sum,num ;
void add(int x,int y,int z){
edge[++tot]=z,ver[tot]=y,Next[tot]=head[x],head[x]=tot ;
}
priority_queue<pair<int,int> > q;
void dijkstra(int start){
while(q.size())q.pop() ;
memset(v,0,sizeof(v)) ;
d[start][start]=0;
q.push(make_pair(d[start][start],start)) ;
while(q.size()){
int x = q.top().second ;
q.pop() ;
if(v[x])continue ;
v[x]=true ;
for(int i = head[x]; i;i = Next[i]){
int y = ver[i] , z = edge[i] ;
if(d[start][y] > d[start][x] + z){
d[start][y] = d[start][x] + z ;
q.push(make_pair(-d[start][y],y)) ;
}
}
}
}
int main(){
memset(d,0x3f,sizeof(d));
scanf("%d%d%d%d",& N,& M,& K,& Query) ;
for(int i=1;i<=M;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z) ;
}
for(int i=1;i<=K;++i){
scanf("%d",& temp[i]) ;
intemp[temp[i]] = true;
dijkstra(temp[i]) ;
//for(int j=1;j<=N;++j)printf("%d ",d[temp[i]][j]) ;printf("\n") ;
}
while(Query--){
int x,y;
scanf("%d%d",&x,&y) ;
int minv = 0x7f7f7f7f ;
for(int i=head[x]; i;i=Next[i]){
//printf("(%d,%d)\n",i,ver[i]) ;
if(intemp[ver[i]])minv = min(minv,edge[i]+d[ver[i]][y]) ;
}
if(minv!=0x7f7f7f7f)sum+=minv,++num/*,printf("can\n")*/ ;
}
printf("%d\n%d\n",num,sum);
return 0;
}
Warning!
本文由 TYQ 创作,采用 知识共享署名 4.0 国际许可协议进行许可。
转载要与作者联系,并需在正文明显处署名作者且注明文章出处。
对了,我永远喜欢C++啊。
Graph & Tree2的更多相关文章
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Clone Graph 无向图的复制
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...
- 讲座:Influence maximization on big social graph
Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
- Theano Graph Structure
Graph Structure Graph Definition theano's symbolic mathematical computation, which is composed of: A ...
随机推荐
- [GXYCTF2019]BabyUpload
0x00 知识点 文件类型绕过: Content-Type: image/jpeg apache环境下上传.hatcess 首先上传一个.htaccess内容如下的文件 :SetHandler app ...
- Java的优先队列PriorityQueue详解
一.优先队列概述 优先队列PriorityQueue是Queue接口的实现,可以对其中元素进行排序, 可以放基本数据类型的包装类(如:Integer,Long等)或自定义的类 对于基本数据类型的包装器 ...
- javascript 解决provisional headers are shown的过程
请求没有被发送,因为是载入缓存资源. 大概是说 完全相同的请求间隔数毫秒(太短),导致加载失败,查看了chrome控制台发现 Provisional headers are shown 出现在 载入缓 ...
- windows Driver 查询指定键值
NTSTATUS status; HANDLE hKey = NULL; OBJECT_ATTRIBUTES oa; UNICODE_STRING strPath = RTL_CONSTANT_STR ...
- Vue 获取时间戳返回自定义时间格式
直接在Vue全局函数定义: Vue.prototype.padLeftZero = function(str) { return ('00' + str).substr(str.length); }; ...
- 66.Python中startswith和endswith的使用
定义模型的models.py,示例代码如下: from django.db import models class Category(models.Model): name = models.Char ...
- 四、CI框架之通过URL路径访问C中的函数
一.在C中写一个test001函数 二.在路径http://127.0.0.1/CodeIgniter-3.1.10/index.php/welcome/test001中访问 不忘初心,如果您认为这篇 ...
- iOS下JS与原生的交互一
本篇主要讲的是UIWebView和JS的交互,在下一节会有wkWebView和JS交互的详解https://www.cnblogs.com/llhlj/p/9144110.html JS调用原生OC ...
- SQL常用短语小记-持续更新
创建链接服务器语句 --//创建链接服务器[在本地服务器创建] exec sp_addlinkedserver '链接服务器名称','','SQLOLEDB','远程服务器地址' -- exec sp ...
- matplotlib画图实例-day1
matplotlib主要用于根据数据画各种图表 官网:https://matplotlib.org/gallery/index.html 例1:画一天中每个两个小时温度变化趋势图 #!/usr/bin ...