Hdu3072-Intelligence System(强连通求最小值)
Input
Output
Sample Input
3 3
0 1 100
1 2 50
0 2 100
3 3
0 1 100
1 2 50
2 1 100
2 2
0 1 50
0 1 100
Sample Output
150
100
50
题意:简单点说就是求把所有强连通分量连在一起所需的最小花费
解析:先把所有强连通分量求出来,再求不同连通分量连接起来的最小花费,最后把除0所在的连通分量所需的最小花费连接起来,
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+;
const int eps=0.0000001;
typedef __int64 LL;
const int maxn=;
const int maxm=;
int N,M,ans,id;
int dfn[maxn],low[maxn],cost[maxn],resign[maxn];
vector<int> G[maxn];
stack<int> KK;
bool inq[maxn];
struct edge
{
int u,v,w;
edge(int u=,int v=,int w=):u(u),v(v),w(w){}
}E[maxm];
void init()
{
ans=id=;
while(!KK.empty()) KK.pop();
for(int i=;i<=N;i++)
{
dfn[i]=low[i]=;
resign[i]=;
cost[i]=INF;
G[i].clear();
inq[i]=false;
}
}
void Tarjan(int x)
{
dfn[x]=low[x]=++id;
inq[x]=true;
KK.push(x);
int t,Size=G[x].size();
for(int i=;i<Size;i++)
{
t=G[x][i];
if(!dfn[t])
{
Tarjan(t);
low[x]=min(low[x],low[t]);
}
else if(inq[t]) low[x]=min(low[x],dfn[t]);
} //前面都差不多
if(dfn[x]==low[x])
{
ans++;
do
{
t=KK.top(); KK.pop();
inq[t]=false;
resign[t]=ans; //这个地方,标记连通分量
}while(t!=x);
}
return;
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
init();
int u,v,w;
for(int i=;i<=M;i++)
{
scanf("%d%d%d",&u,&v,&w);
E[i]=edge(u,v,w);
G[u].push_back(v); //有向图
}
for(int i=;i<N;i++) if(!dfn[i]) Tarjan(i);
for(int i=;i<=M;i++)
{
edge& e=E[i];
int u=e.u,v=e.v,w=e.w;
int x=resign[u],y=resign[v]; //连通分量繁荣编号
if(x!=y) cost[y]=min(cost[y],w); //更新值
}
int sum=;
for(int i=;i<=ans;i++)
{
if(i==resign[]||cost[i]==INF) continue;//跟0是统一连通分量的不管
sum+=cost[i];
}
printf("%d\n",sum);
}
return ;
}
Hdu3072-Intelligence System(强连通求最小值)的更多相关文章
- hdu 3072 Intelligence System(Tarjan 求连通块间最小值)
Intelligence System Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU3072 Intelligence System
题目传送门 有个中文版的题面...和原题稍有不同 /* Description “这一切都是命运石之门的选择.” 试图研制时间机器的机关SERN截获了中二科学家伦太郎发往过去的一条短信,并由此得知了伦 ...
- [HDU3072]:Intelligence System(塔尖+贪心)
题目传送门 题目描述 “这一切都是命运石之门的选择.”试图研制时间机器的机关SERN截获了中二科学家伦太郎发往过去的一条短 信,并由此得知了伦太郎制作出了电话微波炉(仮).为了掌握时间机器的技术,SE ...
- hdu3072 Intelligence System (最小树形图?)
题意:给一个有向图,问要从0号点能到达所有点所需要经过路径的最小权值和是多少,然而,若两点强联通,则这两点互相到达不需要花费.保证0号点能到达所有点 tarjan缩点以后直接取每个点入边中花费最小的即 ...
- HDU 3072 Intelligence System (强连通分量)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Intelligence System (hdu 3072 强联通缩点+贪心)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
随机推荐
- 【POJ2136】Vertical Histogram(简单模拟)
比较简单,按照样例模拟就好!~ #include <iostream> #include <cstdlib> #include <cstdio> #include ...
- 第34讲 UI组件之 ProgressDialog和Message
第34讲UI组件之 ProgressDialog和Message 1.进度对话框 ProgressDialog <1>简介 ProgressDialog是AlertDialog类的一个扩展 ...
- python删除指定位置 2个元素
# -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#排序说明:http://en.wikipedia.org/wiki/i ...
- PHP设计模式笔记五:策略模式 -- Rango韩老师 http://www.imooc.com/learn/236
策略模式 1.概述:策略模式,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,这种模式称为策略模式 例如:一个电商网站系统,针对男性女性用户要各自跳转到不同的商品类目,并且所有广告位展示不 ...
- iOS 8 自适应 Cell
在使用 table view 的时侯经常会遇到这样的需求:table view 的 cell 中的内容是动态的,导致在开发的时候不知道一个 cell 的高度具体是多少,所以需要提供一个计算 cell ...
- _js day12
- easyui-combobox默认值绑定
$('#combox_role').combobox({ panelHeight: , url: '../../Handler/GetComboxItems.ashx?type=0', valueFi ...
- F# 天生就是就异步和并行的料
做模型开发免不了要使用异步和并行计算,尤其在多核CPU的今天,更是如此,F#恰逢其时,天生就具备这种能力,先看一个例子. open System open System.Drawing open Sy ...
- 使用区域组织 ASP.NET MVC 应用程序
MVC 模式可将应用程序的模型(数据)逻辑与其呈现逻辑和业务逻辑分离. 在 ASP.NET MVC 中,这种逻辑分离还在项目结构中以物理方式实现,在该项目结构中,控制器和视图保存在使用命名约定定义关系 ...
- VPS,虚拟主机,云主机,独立服务器区别
作者:张朝权链接:http://www.zhihu.com/question/25507629/answer/105594087来源:知乎著作权归作者所有,转载请联系作者获得授权. 独立服务器独立 ...