Intelligence System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1484 Accepted Submission(s): 655
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
Source
2009 Multi-University Training Contest 17 - Host by NUDT
Recommend
lcy | We have carefully selected several similar problems for you: 3069 3077 3070 3071 3073
强连通缩点+贪心,简单题,但是注意有个坑,如果缩点后就只有1个点的话,代价应该是0
#include<map>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 50010;
const int M = 100010;
const int inf = 0x3f3f3f3f;
int DFN[N];
int low[N];
int block[N];
int Stack[N];
int in[N];
bool instack[N];
int head[N];
int cost[N];
int min_price[N];
int tot, sccnum, index, top, n, m;
struct node
{
int weight;
int next;
int to;
}edge[M];
void addedge(int from, int to, int weight)
{
edge[tot].weight = weight;
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
}
void tarjan(int u)
{
DFN[u] = low[u] = ++index;
Stack[top++] = u;
instack[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (DFN[v] == 0)
{
tarjan(v);
if (low[u] > low[v])
{
low[u] = low[v];
}
}
else if (instack[v])
{
if (low[u] > DFN[v])
{
low[u] = DFN[v];
}
}
}
if (DFN[u] == low[u])
{
sccnum++;
do
{
top--;
block[Stack[top]] = sccnum;
instack[Stack[top]] = 0;
}while (Stack[top] != u);
}
}
void solve()
{
memset( instack, 0, sizeof(instack) );
memset( DFN, 0, sizeof(DFN) );
memset( low, 0, sizeof(low) );
memset( in, inf, sizeof(in) );
memset( min_price, inf, sizeof(min_price) );
sccnum = index = top = 0;
for (int i = 0; i < n; i++)
{
if (DFN[i] == 0)
{
tarjan(i);
}
}
if(sccnum == 1)
{
printf("0\n");
return ;
}
for (int i = 0; i < n; i++)
{
for (int j = head[i]; j != -1; j = edge[j].next)
{
if(block[i] != block[edge[j].to])
{
in[block[edge[j].to]] = min(in[block[edge[j].to]], edge[j].weight);
}
}
}
__int64 ans = 0;
for (int i = 1; i <= sccnum; i++)
{
if(in[i] != inf)
{
ans += (__int64)in[i];
}
}
int mins = inf;
for (int j = head[0]; j != -1; j = edge[j].next)
{
if(block[0] == block[edge[j].to])
{
mins = min(mins, edge[j].weight);
}
}
if(mins != inf)
ans += (__int64)mins;
printf("%I64d\n", ans);
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
memset( head, -1, sizeof(head) );
tot = 0;
int u, v, w;
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
}
solve();
}
return 0;
}