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
题意
题意就是给我们一张图 然后同一个强连通分量中传播消息不需要权值 即一个强连通分量中只加一个权 求遍历整张图的最小权 首先我们可以很容易的想到最小生成树 可以吗 显然不可以 因为同一个强连通分量只计算一个权值 所以自然而然想到缩点 我们得到一张AUG图 最小生成树? 不这是一张有向图 所以我们需要用最小树型图来完成(暴力求解也可 缩点之后点很少(这道题没有极限数据))
AC代码
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<utility>
#include<cstring>
using namespace std;
int m,n;
int tmpa,tmpb,tmpc;
int edge[300][300];
vector<int>G[51000];
stack<int>sta;
set<int>se;
const int INF = 0x3f3f3f3f;
int xx[51000*2],yy[51000*2],zz[51000*2];
int dfn[51000];
int low[51000];
int id[51000];
int sum,flag,ans;
int Tarjan(int start){
se.insert(start);
sta.push(start);
dfn[start] = low[start] = ++flag;
int size_All = G[start].size();
for(int i=0;i<size_All;++i){
int x = G[start][i];
if(dfn[x] == -1){
Tarjan(x);
low[start] = min(low[start],low[x]);
}else if(se.find(x) != se.end()){
low[start] = min(low[start],low[x]);
}
}
if(dfn[start] == low[start]){
while(1){
int x = sta.top();
sta.pop();
se.erase(x);
id[x] = sum;
if(start == x) break;
}
++sum;
}
}
void solve(){
bool vis[51000];
for(int i=0;i<n;i++){
int a = xx[i],b = yy[i];
if(id[a] != id[b]){
edge[id[a]][id[b]] = min(edge[id[a]][id[b]],zz[i]);
}
}
memset(vis,false,sizeof(vis));
for(int i=0;i<sum;++i){
int temp = INF,tmp=0;
for(int j=0;j<sum;++j){
for(int k=0;k<sum;++k){
if(!vis[k] && edge[j][k]<temp){
temp = edge[j][k];
tmp = k;
}
}
}
if(temp != INF)
ans+=temp;
vis[tmp] = true;
}
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d %d",&m,&n)){
for(int i=0;i<m;++i) G[i].clear();
for(int i=0;i<n;++i){
scanf("%d %d %d",&xx[i],&yy[i],&zz[i]);
G[xx[i]].push_back(yy[i]);
}
memset(dfn,-1,sizeof(dfn));
memset(low,0,sizeof(low));
memset(id,0,sizeof(id));
while(!sta.empty()) sta.pop();
sum = 0; flag = -1,ans = 0;
se.clear();
for(int i=0;i<m;++i){
if(dfn[i] == -1) Tarjan(i);
}
memset(edge,INF,sizeof(edge));
solve();
}
return 0;
}