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的更多相关文章

  1. 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 ...

  2. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  3. poj 3411 Paid Roads(dfs)

    Description A network of m roads connects N cities (numbered to N). There may be more than one road ...

  4. [jzoj5786]【NOIP2008模拟】观察 (dfs序+lca)

    传送门 Description infleaking十分愉快地走在路上, 因为经过10^9^9^9年后, 他得到了一个新技能--观察大法. 刚出来的infleaking就想要挑战自我. 为什么infl ...

  5. codeforces 711D Directed Roads(DFS)

    题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...

  6. POJ 3411 Paid Roads(SPFA || DFS)

    题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...

  7. Codeforces Gym101246G:Revolutionary Roads(DFS+思维)

    http://codeforces.com/gym/101246/problem/G 题意:有一个n个点m条边的有向图,现在可以修改某一条有向边使得其为无向边,问修改哪些边可以使得修改后的强连通分量的 ...

  8. 【割点】【割边】tarjan

    洛谷割点模板题--传送门 割边:在连通图中,删除了连通图的某条边后,图不再连通.这样的边被称为割边,也叫做桥.割点:在连通图中,删除了连通图的某个点以及与这个点相连的边后,图不再连通.这样的点被称为割 ...

  9. Tarjan求有向图强连通详解

    Tarjan求有向图强连通详解 注*该文章为转发,原文出处已经不得而知 :first-child { margin-top: 0; } blockquote > :last-child { ma ...

随机推荐

  1. java 提高效率的做法

    可供程序利用的资源(内存.CPU时间.网络带宽等)是有限的,优化的目的就是让程序用尽可能少的资源完成预定的任务.优化通常包含两方面的内容:减小代码的体积,提高代码的运行效率.本文讨论的主要是如何提高代 ...

  2. Linux下编译安装源码包软件 configure ,make, make install, make test/check, make clean

    http://www.360doc7.net/wxarticlenew/541275971.html 一.什么是源码包软件? 顾名思义,源码包就是源代码的可见的软件包,基于Linux和BSD系统的软件 ...

  3. spring cloud config 属性加解密

    首先需要(Java Cryptography Extension (JCE))的支持,下载路径: https://www.oracle.com/technetwork/java/javase/down ...

  4. RedisCluster集群原理

    主从复制,数据值每个服务器都存了. 针对redis集群的解决方案, 连接这个集群,不用在乎Master了 6台redis 1.why use Redis? 减轻数据库访问压力 2.持久化 RDB(间隔 ...

  5. golang defer使用——资源关闭时候多用

    defer Go语言中有种不错的设计,即延迟(defer)语句,你可以在函数中添加多个defer语句.当函数执行到最后时,这些defer语句会按照逆序执行,最后该函数返回.特别是当你在进行一些打开资源 ...

  6. [原创]java实现word转pdf

    最近遇到一个项目需要把word 转成pdf,百度了一下网上的方案有很多,比如虚拟打印.给word 装扩展插件等,这些方案都依赖于ms word 程序,在java代码中也得使用诸如jacob或jcom这 ...

  7. AM335x Android eMMC mkmmc-android.sh hacking

    # AM335x Android eMMC mkmmc-android.sh hacking # # . 有空解读一下android的分区文件. # . 代码来源:https://github.com ...

  8. Java内存溢出?

    内存溢出? https://www.cnblogs.com/crossoverJie/p/9552119.html 前言 OutOfMemoryError 问题相信很多朋友都遇到过,相对于常见的业务异 ...

  9. TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

    报错原因:numpy不能读取CUDA tensor 需要将它转化为 CPU tensor. 所以如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tenso ...

  10. Integrate Your Code with the Frameworks---整合你的代码和框架

    Back to Frameworks Integrate Your Code with the Frameworks When you develop an app for OS X or iOS, ...