1049 - One Way Roads 观察 dfs
http://lightoj.com/volume_showproblem.php?problem=1049
题意是,在一副有向图中,要使得它变成一个首尾相连的图,需要的最小代价。
就是本来是1-->2 2-->3 1--->3的,变成1-->2-->3--->1的话,需要把1-->3变成3--->1,就要耗费这条边的代价
思路就是找出一个入度为2的点,要么往上走,要么往下走,dfs两次。
或者记录一个总和,dfs一次就好,上一次没耗费的,正是向下走要耗费的
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
struct node {
int u, v, w, tonext;
bool flag;
} e[maxn * ];
int first[maxn];
int num;
int in[maxn];
int DFN;
void add(int u, int v, int w, bool flag) {
++num;
e[num].u = u;
e[num].v = v;
e[num].w = w;
e[num].tonext = first[u];
first[u] = num;
e[num].flag = flag;
}
int now;
int dfs(int cur, int root, int fa) {
if (cur == root && now == ) return ;
if (cur == root) {
for (int i = first[cur]; i; i = e[i].tonext) {
now--;
if (now == ) {
return e[i].w + dfs(e[i].v, root, cur);
}
}
} else {
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (v == fa) continue;
if (e[i].flag) {
return dfs(v, root, cur);
} else {
return e[i].w + dfs(v, root, cur);
}
}
}
}
void work() {
num = ;
++DFN;
int n;
scanf("%d", &n);
int root = -inf;
for (int i = ; i <= n; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
if (in[v] == DFN) {
root = v;
}
in[v] = DFN;
add(u, v, w, true);
add(v, u, w, false);
}
int ans = inf;
if (root == -inf) {
ans = ;
} else {
now = ;
ans = dfs(root, root, );
now = ;
ans = min(ans, dfs(root, root, ));
}
static int f = ;
printf("Case %d: %d\n", ++f, ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
1049 - One Way Roads 观察 dfs的更多相关文章
- lightoj 1049 - One Way Roads(dfs)
Time Limit: 0.5 second(s) Memory Limit: 32 MB Nowadays the one-way traffic is introduced all over th ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- poj 3411 Paid Roads(dfs)
Description A network of m roads connects N cities (numbered to N). There may be more than one road ...
- [jzoj5786]【NOIP2008模拟】观察 (dfs序+lca)
传送门 Description infleaking十分愉快地走在路上, 因为经过10^9^9^9年后, 他得到了一个新技能--观察大法. 刚出来的infleaking就想要挑战自我. 为什么infl ...
- codeforces 711D Directed Roads(DFS)
题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...
- POJ 3411 Paid Roads(SPFA || DFS)
题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...
- Codeforces Gym101246G:Revolutionary Roads(DFS+思维)
http://codeforces.com/gym/101246/problem/G 题意:有一个n个点m条边的有向图,现在可以修改某一条有向边使得其为无向边,问修改哪些边可以使得修改后的强连通分量的 ...
- 【割点】【割边】tarjan
洛谷割点模板题--传送门 割边:在连通图中,删除了连通图的某条边后,图不再连通.这样的边被称为割边,也叫做桥.割点:在连通图中,删除了连通图的某个点以及与这个点相连的边后,图不再连通.这样的点被称为割 ...
- Tarjan求有向图强连通详解
Tarjan求有向图强连通详解 注*该文章为转发,原文出处已经不得而知 :first-child { margin-top: 0; } blockquote > :last-child { ma ...
随机推荐
- NameNode备份策略以及恢复方法
一.dits和fsimage 首先要提到两个文件edits和fsimage,下面来说说他们是做什么的. 集群中的名称节点(NameNode)会把文件系统的变化以追加保存到日志文件edits中 ...
- oracle的索引有几种?各有何用途?
1. b-tree索引Oracle数据库中最常见的索引类型是b-tree索引,也就是B-树索引,以其同名的计算科学结构命名.CREATE INDEX语句时,默认就是在创建b-tree索引.没有特别规定 ...
- How MySQL Opens and Closes Tables refuse connections 拒绝连接的原因 file descriptors
MySQL :: MySQL 5.7 Reference Manual :: 8.4.3.1 How MySQL Opens and Closes Tables https://dev.mysql.c ...
- 20170223 遇到自建表里面相同key值数据不唯一
我怎么发现这个表里 key值相同数据不唯一, 这两条看起来是完全相同的, 其实排序不能能合并已经说明问题.
- IE67 下 setattribute class 失效
解决办法.将class 换成 className ,同理.ff不能识别className,将其换成class element.setAttribute("class"," ...
- 一步一步学Silverlight 2系列文章
概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...
- Android API中的对话框
Android API中提供了四个Dialog的自定义子类: AlertDialog ProgressDialog DatePackerDialog TimePickerDialog 也可以派生出自己 ...
- skynet源码阅读<6>--线程调度
相比于上节我们提到的协程调度,skynet的线程调度从逻辑流程上来看要简单很多.下面我们就来具体做一分析.首先自然是以skynet_start.c为入口: static void start(int ...
- 【NOIP2016】 组合数问题
[题目链接] 点击打开链接 [算法] 杨辉三角 + 二维前缀和 O(1)计算答案 [代码] #include<bits/stdc++.h> using namespace std; #de ...
- linux--vsftpd的安装和配置(转)
Linux下如何进行FTP设置(转) [TOC] Redhat/CentOS安装vsftp软件 1. 安装vsftp $ yum install vsftpd -y 2. 添加ftp帐号和目录 先检查 ...