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. Gym - 100341C FFT优化DP

    题目链接:传送门 题解: 设定dp[i][j]在深度为i下,使用j个节点的方案数 显然的转移方程组就是 dp[h][n] = dp[h-1][i] * dp[h-1][n-i-1] + 2*dp[h- ...

  2. hibernate 的分页查询

    hibernate的分页查询有个好处,就是不用管数据库方言.比如db2的分页查询很麻烦,但是用hibernate的方式,就完全不用管这些了 /* 使用HQL分页查询Customer信息 */ publ ...

  3. Ubuntu 配置 nfsserver

    https://help.ubuntu.com/lts/serverguide/network-file-system.html#nfs-installation 网络文件系统 (NFS) NFS 允 ...

  4. js生成随机编码并赋值给input文本框

    效果图如下: 页面代码: <div class="form-item form-width-in fr"> <label>产 品 编 码</label ...

  5. codeforces 445B. DZY Loves Chemistry 解题报告

    题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...

  6. Python学习笔记_Redis

    一.Redis介绍 非关系型数据库:mongodb.redis redis数据全部是存在内存里面的. redis本身每秒支持30w次的读写,本身性能非常好, redis重启后,数据会消失,持久性不好, ...

  7. jstl标签: c:Foreach详解

    为循环控制,它可以将集合(Collection)中的成员循序浏览一遍.运作方式为当条件符合时,就会持续重复执行的本体内容. 为循环控制,它可以将集合(Collection)中的成员循序浏览一遍.运作方 ...

  8. SPOJ:Bits. Exponents and Gcd(组合数+GCD)

    Rastas's has been given a number n. Being weak at mathematics, she has to consider all the numbers f ...

  9. [SHOI 2013] 发微博

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4419 [算法] 用std :: set维护每个人的好友集合 当两人成为好友时将每人接 ...

  10. struts2通用标签

    Struts2框架为我们提供了很多标签,这些标签总体上可以分为两类:通用标签和UI标签.通用标签分为两类:数据标签和控制标签.数据标签用于访问值栈中数据,控制标签用于控制呈现页面时数据执行流程.使用S ...