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;
    }
    
  • 相关阅读:
    大道至简第一张读后感
    字符串加密
    写一个类,在任何时候都可以向它查询创建了多少个类
    类与对象动手动脑
    2016年读书清单
    2016-09-01
    Spring笔记(五)--注解方式实现AOP
    Spring笔记(三)--代理模式
    Spring笔记(四)--公共属性的配置
    表达式之谜
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/11864933.html
Copyright © 2011-2022 走看看