zoukankan      html  css  js  c++  java
  • [CF24A]Ring road(2019-11-15考试)

    题目大意

    给你一个(n)个点的环,每条边有方向,改变第(i)条边的方向代价为(w_i),问将其改为强连通图的最小代价。(nleqslant100)

    题解

    求出把边全部改为顺时针和全部改为逆时针的代价,较少的输出即可

    卡点

    C++ Code:

    
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    const int maxn = 111;
     
    int head[maxn], cnt;
    struct Edge { int to, nxt; } e[maxn << 1];
    void addedge(int a, int b) {
    	e[++cnt] = (Edge) { b, head[a] }; head[a] = cnt;
    	e[++cnt] = (Edge) { a, head[b] }; head[b] = cnt;
    }
     
    int n, dep[maxn], l[maxn], r[maxn], w[maxn], res1, res2;
    void dfs(int u) {
    	for (int i = head[u], v; i; i = e[i].nxt) {
    		v = e[i].to;
    		if (!dep[v]) dep[v] = dep[u] + 1, dfs(v);
    	}
    }
     
     
    int main() {
    	std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
    	std::cin >> n;
    	for (int i = 1; i <= n; ++i) {
    		std::cin >> l[i] >> r[i] >> w[i];
    		addedge(l[i], r[i]);
    	}
    	dfs(dep[1] = 1);
    	for (int i = 1; i <= n; ++i) {
    		if (dep[r[i]] != dep[l[i]] % n + 1) res2 += w[i];
    		else res1 += w[i];
    	}
    	std::cout << std::min(res1, res2) << '
    ';
    	return 0;
    }
    
  • 相关阅读:
    回头再看libpcap
    lex error
    perl 的威力
    什么叫回流和重绘?
    HTTP协议与FTP协议的区别【转】
    Linux操作系统Redhat系列与Debian系列 【转】
    dock是什么? docker就是集装箱原理【转】
    光端机的作用【转】
    c++ -- call_once用法
    c++ -- thread详细解析
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/11864933.html
Copyright © 2011-2022 走看看