I n t e l l i g e n c e S y s t e m Intelligence\ System Intelligence System
Time Limit: 2000/1000 MS (Java/Others) | Memory Limit: 32768/32768 K (Java/Others) |
---|---|
Total Submission(s): 3512 | Accepted Submission(s): 1543 |
Problem Description
After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it is of course for ACM … …
Now, kzc_tc, the head of the Intelligence Department (his code is once 48, but now 0), is sudden obtaining important information from one Intelligence personnel. That relates to the strategic direction and future development of the situation of ALPC. So it need for emergency notification to all Intelligence personnel, he decides to use the intelligence system (kzc_tc inform one, and the one inform other one or more, and so on. Finally the information is known to all).
We know this is a dangerous work. Each transmission of the information can only be made through a fixed approach, from a fixed person to another fixed, and cannot be exchanged, but between two persons may have more than one way for transferring. Each act of the transmission cost Ci (1 <= Ci <= 100000), the total cost of the transmission if inform some ones in our ALPC intelligence agency is their costs sum.
Something good, if two people can inform each other, directly or indirectly through someone else, then they belong to the same branch (kzc_tc is in one branch, too!). This case, it’s very easy to inform each other, so that the cost between persons in the same branch will be ignored. The number of branch in intelligence agency is no more than one hundred.
As a result of the current tensions of ALPC’s funds, kzc_tc now has all relationships in his Intelligence system, and he want to write a program to achieve the minimum cost to ensure that everyone knows this intelligence.
It’s really annoying!
Input
There are several test cases.
In each case, the first line is an Integer N (0< N <= 50000), the number of the intelligence personnel including kzc_tc. Their code is numbered from 0 to N-1. And then M (0<= M <= 100000), the number of the transmission approach.
The next M lines, each line contains three integers, X, Y and C means person X transfer information to person Y cost C.
Output
The minimum total cost for inform everyone.
Believe kzc_tc’s working! There always is a way for him to communicate with all other intelligence personnel.
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
Source
2009 Multi-University Training Contest 17 - Host by NUDT
题目大意:
给出n个点(0~n-1)和m条带权边,要求计算出经过所有点的最小消耗,其中强连通的点之间不需要消耗。
分析:
首先肯定是进行强连通分量分解,然后缩点之后建图(注意题目保证新图的节点少于100),找到起点之后找到最小生成树就好了。
现在就是找DAG的最小生成树的问题,因为是DAG,所以从起点开始往后进行搜索不会回到之前已经到达过的点,也就是到达了某点就不会对它之前点的最小距离的产生影响。其实要做的就是不断更新到达某点的最小消耗值,进行DFS进行更新就好了。
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 5e4+7;
const int maxm = 1e5+7;
const int maxg = 111;
const int INF = 0x3f3f3f3f;
struct EDGE{
int to,next,cost;
};
EDGE edge[maxm];
int dfn[maxn],low[maxn],stk[maxn],instk[maxn],head[maxn],scc_id[maxn],mp[maxg][maxg],indegree[maxg],dist[maxg],used[maxg];
int n,m,top,number,index,edge_num;
void init(){
top = number = index = edge_num = 0;
for(int i=0;i<maxn;i++) dfn[i]=low[i]=instk[i]=0;
memset(head,255,sizeof(head));
}
void add_edge(int u,int v,int c){
edge[edge_num].to = v;
edge[edge_num].cost = c;
edge[edge_num].next = head[u];
head[u] = edge_num++;
}
void input(){
init();
for(int i=1;i<=m;i++){
int u,v,c;
scanf("%d %d %d",&u,&v,&c);
add_edge(u,v,c);
}
}
void tarjan(int u){
dfn[u] = low[u] = ++index;
instk[stk[++top]=u] = 1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v = edge[i].to;
if(!dfn[v]){
tarjan(v);
low[u] = min(low[u],low[v]);
}
else if(instk[v]) low[u] = min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
number++;
while(stk[top]!=u){
instk[stk[top]] = 0;
scc_id[stk[top--]] = number;
}
instk[stk[top]] = 0;
scc_id[stk[top--]] = number;
}
}
void rebuild(){
memset(mp,INF,sizeof(mp));
memset(indegree,0,sizeof(indegree));
for(int i=0;i<n;i++){
for(int j=head[i];j!=-1;j=edge[j].next){
int u = scc_id[i];
int v = scc_id[edge[j].to];
if(u!=v){
mp[u][v] = min(mp[u][v],edge[j].cost);
indegree[v]++;
}
}
}
}
void dfs(int now){
used[now] = 1;
for(int i=1;i<=number;i++) if(mp[now][i]!=INF){
dist[i] = min(dist[i],mp[now][i]);
if(!used[i]) dfs(i);
}
}
int solve(){
memset(used,0,sizeof(used));
memset(dist,INF,sizeof(dist));
int st,ans=0;
for(int i=1;i<=number;i++) if(!indegree[i]){
st = i;
break;
}
dist[st]=0; dfs(st);
for(int i=1;i<=number;i++) ans+=dist[i];
return ans;
}
int main(){
while(scanf("%d %d",&n,&m)!=EOF){
input();
for(int i=0;i<n;i++) if(!dfn[i]) tarjan(i);
rebuild();
printf("%d\n",solve());
}
return 0;
}