强大的dfs(用处1——拓扑排序【xdoj1025】,用处二——求强联通分量【ccf高速公路】)当然dfs用处多着咧
xdoj 1025

题目分析:看着好吓人啊。。其实分析清晰也很简单。每种物体既可以直接买到或者由其他物体制作得到。那么我们就取两者的最小值。制作的花费我们可以这样求得
如果x的原材料有y 那么x和y之间有一条有向边 。然后按照拓扑排序的顺序自底而上求出各个物体制作所需的价值。。很简单吗?!
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N=1e4+;
vector < vector <int> > g(N);
int val[N];
bool vis[N];
int n,m,w;
void dfs (int rt) {
vis[rt]=;
if (g[rt].size()==) return ;// 最底层直接返回
int cost=w;
for (int i=;i<g[rt].size();i++) {
int next=g[rt][i];
if (!vis[next]) dfs(next);
cost+=val[next];//cost制作费用
}
val[rt]=min (cost,val[rt]);//取两者最小值
return ;
}
int main ()
{
int T;
scanf ("%d",&T);
while (T--) {
scanf ("%d %d %d",&n,&m,&w);
for (int i=;i<n;i++) g[i].clear();
memset (vis,,sizeof(vis));
for (int i=;i<n;i++) {
scanf ("%d",&val[i]);
int num; scanf ("%d",&num);
for (int j=;j<num;j++) {
int u; scanf ("%d",&u);
g[i].push_back(u);
}
}
for (int i=;i<n;i++)
if (!vis[i]) dfs (i);
int sum=;
for (int i=;i<m;i++) {
int x; scanf ("%d",&x);
sum+=val[x];
}
printf ("%d\n",sum);
}
return ;
}
ccf 高速公路 求联通分量 这个算法好像叫trajin 向这些科学家致敬
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;
const int N=1e4+;
vector < vector <int> > g(N);
stack <int> ss;
bool instack[N];
bool vis[N];
int dfn[N],low[N];// dfn 保存遍历序号 low 这个点所能到达的最小点
int ans;
int n,m;
int cnt;//访问顺序
void dfs (int rt) {
vis[rt]=;
instack[rt]=; ss.push(rt);
dfn[rt]=low[rt]=++cnt;
for (int i=;i<g[rt].size();i++) {
int next=g[rt][i];
if (!vis[next]) {
dfs (next);
low[rt]=min (low[rt],low[next]);
}
else if (instack[next]) {
low[rt]=min (low[rt],low[next]);
}
}
if (dfn[rt]==low[rt]) {// dfn[rt]==low[rt] 表示遍历一个联通分变量啦
int num=;
int tmp=ss.top(); ss.pop(); instack[tmp]=;//在栈中 说明这个点的后继子孙还没有访问完
while (dfn[tmp]!=low[tmp]) { num++; tmp=ss.top(); ss.pop(); instack[tmp]=;}
ans+=(num-)*num/;
}
return ;
}
int main ()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for (int i=;i<=m;i++) {
int x,y; cin>>x>>y;
g[x].push_back(y);
}
for (int i=;i<=n;i++)
if (!vis[i]) dfs (i);
printf ("%d\n",ans);
return ;
}
强大的dfs(用处1——拓扑排序【xdoj1025】,用处二——求强联通分量【ccf高速公路】)当然dfs用处多着咧的更多相关文章
- Vijos P1023Victoria的舞会3【贪心+DFS求强联通分量】
链接:Click Me! P1023Victoria的舞会3 Accepted 标签:Victoria的舞会[显示标签] 描写叙述 Victoria是一位颇有成就的艺术家,他因油画作品<我爱北京 ...
- CodeForces 1213F (强联通分量分解+拓扑排序)
传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...
- 【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs' Box Boutique
给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排 ...
- hihoCoder #1185 : 连通性·三(强联通分量+拓扑排序)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- 拓扑排序详解(梅开二度之dfs版按字典序输出拓扑路径+dfs版输出全部拓扑路径
什么是拓扑排序? 先穿袜子再穿鞋,先当孙子再当爷.这就是拓扑排序! 拓扑排序说白了其实不太算是一种排序算法,但又像是一种排序(我是不是说了个废话qwq) 他其实是一个有向无环图(DAG, Direct ...
- uvaLA4255 Guess BFS+拓扑排序
算法指南白书 思路:“连续和转化成前缀和之差” #include <stdio.h> #include <string.h> #include <iostream> ...
- Poj 3683-Priest John's Busiest Day 2-sat,拓扑排序
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8872 Accept ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- CF798E. Mike and code of a permutation [拓扑排序 线段树]
CF798E. Mike and code of a permutation 题意: 排列p,编码了一个序列a.对于每个i,找到第一个\(p_j > p_i\)并且未被标记的j,标记这个j并\( ...
随机推荐
- NHibernate 映射关系
基本映射关系如下: NHibernate类型 .NET类型 Database类型 备注 AnsiChar System.Char DbType.AnsiStringFixedLength - 1 ch ...
- 个人学期总结及Python+Flask+MysqL的web建设技术过程
一个学期即将过去,我们也迎来了2018年.这个学期,首次接触了web网站开发建设,不仅是这门课程,还有另外一门用idea的gradle框架来制作网页. 很显然,用python语言的flask框架更加简 ...
- 安卓本地化之SharedPreferences
SharedPreferences的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息,用Sqlite数据库来存放并不划算,因为数据库连接跟操作等耗时大大影响了程序的 ...
- Krapo 2
The krpano Viewer is a small and very flexible high-performance viewer for all kind of panoramic ima ...
- 操作系统错误 5:"5(拒绝访问。)
------------------------------ 无法打开物理文件 "G:/QGJX.mdf".操作系统错误 5:"5(拒绝访问.)". (Micr ...
- IDEA2017安装actibpmn插件中文乱码问题解决
1.修改idea安装目录下的两个文件 C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.4\bin\idea.exe.vmoptions C:\Progr ...
- STM 软件事务内存——本质是为提高并发,通过事务来管理内存的读写访问以避免锁的使用
对Java程序员来说,我们对面向对象的编程(OOP)自然都是烂熟于胸的,但语言也极大地影响了我们构建面向对象应用程序的方式.(现在的OOP已经和Alan Kay当初创造这个词时候的初衷大不相同了,他的 ...
- win10启动移动热点解决办法
netsh wlan start hostednetwork C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup gpedit.msc
- mkfs.ext3 option
mkfs.ext3 OPTIONS -b block-size Specify the size of blocks in bytes. Valid block-size values are ...
- cookie -- 添加删除
前段时间学到了cookie,之前的公司用的jquery插件,现在终于学到了原生的js <!doctype html> <html> <head> <meta ...