hdu3072 Intelligence System【强连通】
来源:互联网 发布:好玩的java源代码 编辑:程序博客网 时间:2024/05/23 00:01
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3072
题意:有n个人,0要把消息传给其他人,但是他们只会把消息传给认识的人,每个传递消息的人都必须花费一定的价值,但是如果若干个人相互之间都可以传递消息的话,就不用花钱,问你最少花费多少,使得0号能把消息传给所有人
解析:首先如果相互认识就不用花钱,那么先强连通缩点,构成一个无环有向图,然后维护每条边的最小值,然后求和即是答案
#include <bits/stdc++.h>using namespace std;const int maxn=1e5+100;const int inf = 0x3f3f3f3f;vector<pair<int,int> >G[maxn];vector<int>rG[maxn];vector<int>vs;int vis[maxn],cmp[maxn];int ans[maxn];void init(int n){ for(int i=0;i<n;i++) { G[i].clear(); rG[i].clear(); }}void addEdge(int from,int to,int c){ G[from].push_back(make_pair(to,c)); rG[to].push_back(from);}void dfs(int u){ vis[u] = 1; for(int i=0;i<(int)G[u].size();i++) { int v = G[u][i].first; if(!vis[v]) dfs(v); } vs.push_back(u);}void rdfs(int u,int k){ vis[u] = 1; cmp[u] = k; for(int i=0;i<(int)rG[u].size();i++) { int v = rG[u][i]; if(!vis[v]) rdfs(v,k); }}int scc(int n){ memset(vis,0,sizeof(vis)); vs.clear(); for(int i=0;i<n;i++) { if(!vis[i]) dfs(i); } memset(vis,0,sizeof(vis)); int k = 0; for(int i=n-1;i>=0;i--) { if(!vis[vs[i]]) rdfs(vs[i],k++); } return k;}int main(void){ int n,m; while(~scanf("%d %d",&n,&m)) { init(n); for(int i=0;i<m;i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); addEdge(a,b,c); } int k = scc(n); memset(ans,inf,sizeof(ans)); for(int i=0;i<n;i++) { for(int j=0;j<(int)G[i].size();j++) { int v = G[i][j].first; int c = G[i][j].second; if(cmp[i]!=cmp[v]) ans[cmp[v]] = min(ans[cmp[v]],c); } } int res = 0; ans[cmp[0]] = 0; for(int i=0;i<k;i++) res += ans[i]; printf("%d\n",res); } return 0;}
阅读全文
1 0
- hdu3072 Intelligence System【强连通】
- HDU3072 Intelligence System(强连通+最小传递费用+边权)
- hdu3072 Intelligence System (Tarjan)
- hdu3072 Intelligence System
- HDU3072 Intelligence System
- hdu3072 Intelligence System
- hdu3072——Intelligence System
- [JZOJ4686] 通讯 [HDU3072] Intelligence System
- HDU 3072 Intelligence System(强连通分量)
- hdu 3072 Intelligence System(强连通分量)
- HDU 3072 Intelligence System (强连通分量)
- HDU 3072 Intelligence System(强连通分量)
- Intelligence System (强连通 + 缩点)
- HDU3072 Intelligence System【最小树形图】
- HDU 3072 Intelligence System 强连通 最小树形图
- HDU 3072 Intelligence System(强连通+最小树形图)
- hdu 3072 Intelligence System (强连通,缩点)
- hdu3072 Intelligence System(tarjan缩点+最小树形图)
- tensorflow报错:Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul')
- 【shell】结构控制-循环语句
- 【PAT 1001Public Bike Management (30)】+ dfs + dijkstra
- Unity3D Vector3.Dot 点乘 与 Vector3.Cross 叉乘
- C++学习笔记之数组
- hdu3072 Intelligence System【强连通】
- jQuery添加节点___例_添加表格
- PAT 1069微博转发抽奖
- Ceres-Solver学习笔记(3)
- 暑期集训之喷水装置一
- 【BZOJ】4554 [Tjoi2016&Heoi2016]游戏 二分图
- NYOJ 144 小柯的烦恼
- ssh三大框架整合时struts2遇到的简单又很容易忽视的问题
- main函数参数_可变参数