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. mongo 原理 Replica Set Oplog

    [客户的写请求全部发送至主节点] Primary. The primary receives all write operations. Secondaries. Secondaries replic ...

  2. inheritance super overrides printMethod in Superclass override重写父方法

    Core Java Web Page http://horstmann.com/corejava.html [ inheritance ] package v1ch05.inheritance; im ...

  3. 安卓开发中使用ZXing生成解析二维码

    编码示例 package com.wolf_pan.qrcodesample; import android.graphics.Bitmap; import android.graphics.Colo ...

  4. UsbManager, UsbDevice的简单示例

    activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  5. Tomcat的虚拟主机的配置

    比如:配置一个虚拟主机的名字是www.sina.com 1 改动window系统中的HOST文件[C:\WINDOWS\system32\drivers\etc\hosts]   127.0.0.1 ...

  6. BAPI_PO_CEATE 与PO_1

  7. Ruby map、each、select、inject、collect 、detect reference

    参考 https://ruby-china.org/topics/26718 map:(collect是map的别名函数) 对数组中每个元素进行表达式操作,原始数组不会被改变,返回执行表达式结果的新数 ...

  8. Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland

    C. Roads in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. react native 中的redux

    一.使用redux 的条件: 1.某个组件的状态,需要共享: 2.某个状态需要在任何地方都可以拿到: 3.一个组件需要改变全局状态: 4.一个组件需要改变另一个组件的状态. redux   说明白点, ...

  10. Spring Boot2.0+Redis+Ehcache实现二级缓存

    EHCache 本地缓存 Redis 分布式缓存(可以共享) 一级 Redis 二级Ehcache    当redis挂了 有备胎 反之: 先走本地,本地没有再走网络  尽量少走Redis  效率会高 ...