大意:听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间和电话费了。他知道其他人也有一些别人的联系方式,这样他可以通知其他人,再让其他人帮忙通知一下别人。你能帮Wiskey计算出至少要通知多少人,至少得花多少电话费就能让所有人都被通知到吗?
思路:强连通+缩点,然后统计入度为0的点,然后枚举每个SCC中花费最小的值相加,即cost[belong[i]] = min(cost[belong[i]], money[i]);
CODE:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int MAXN = 1010;
const int MAXM = 2010;
const int INF = 0x3f3f3f3f;
struct Edge
{
int v, next;
}edge[MAXM];
int n, m;
int cnt;
int tot, scnt, top, ans;
int first[MAXN], stack[MAXN], ins[MAXN], low[MAXN], dfn[MAXN];
int ind[MAXN], outd[MAXN];
int belong[MAXM];
int money[MAXN], cost[MAXN];
inline void init()
{
cnt = 0;
tot = scnt = top = 0;
ans = 0;
memset(first, -1, sizeof(first));
memset(dfn, 0, sizeof(dfn));
memset(ins, 0, sizeof(ins));
memset(ind, 0, sizeof(ind));
}
inline void read_graph(int u, int v)
{
edge[cnt].v = v;
edge[cnt].next = first[u], first[u] = cnt++;
}
inline void read_graph2()
{
for(int i = 1; i <= n; i++) scanf("%d", &money[i]);
while(m--)
{
int u, v;
scanf("%d%d", &u, &v);
read_graph(u, v);
}
}
inline void dfs(int u)
{
int v;
dfn[u] = low[u] = ++tot;
ins[u] = 1;
stack[top++] = u;
for(int e = first[u]; e != -1; e = edge[e].next)
{
v = edge[e].v;
if(!dfn[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u])
{
scnt++;
do
{
v = stack[--top];
belong[v] = scnt;
ins[v] = 0;
}while(v != u);
}
}
inline void Tarjan()
{
for(int v = 1; v <= n; v++) if(!dfn[v])
dfs(v);
}
inline void solve()
{
Tarjan();
for(int u = 1; u <= n; u++)
{
for(int e = first[u]; e != -1; e = edge[e].next)
{
int v = edge[e].v;
if(belong[u] != belong[v])
{
ind[belong[v]]++;
}
}
}
int SCC = 0;
for(int i = 1; i <= scnt; i++)
{
if(ind[i] == 0) SCC++;
cost[i] = INF;
}
for(int i = 1; i <= n; i++)
{
if(ind[belong[i]] == 0)
{
cost[belong[i]] = min(cost[belong[i]], money[i]);
}
}
for(int i = 1; i <= scnt; i++) if(cost[i] != INF)
{
ans += cost[i];
}
printf("%d %d\n", SCC, ans);
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
init();
read_graph2();
solve();
}
return 0;
}
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int MAXN = 1010;
const int MAXM = 2010;
const int INF = 0x3f3f3f3f;
struct Edge
{
int v, next;
}edge[MAXM];
int n, m;
int cnt;
int tot, scnt, top, ans;
int first[MAXN], stack[MAXN], ins[MAXN], low[MAXN], dfn[MAXN];
int ind[MAXN], outd[MAXN];
int belong[MAXM];
int money[MAXN], cost[MAXN];
inline void init()
{
cnt = 0;
tot = scnt = top = 0;
ans = 0;
memset(first, -1, sizeof(first));
memset(dfn, 0, sizeof(dfn));
memset(ins, 0, sizeof(ins));
memset(ind, 0, sizeof(ind));
}
inline void read_graph(int u, int v)
{
edge[cnt].v = v;
edge[cnt].next = first[u], first[u] = cnt++;
}
inline void read_graph2()
{
for(int i = 1; i <= n; i++) scanf("%d", &money[i]);
while(m--)
{
int u, v;
scanf("%d%d", &u, &v);
read_graph(u, v);
}
}
inline void dfs(int u)
{
int v;
dfn[u] = low[u] = ++tot;
ins[u] = 1;
stack[top++] = u;
for(int e = first[u]; e != -1; e = edge[e].next)
{
v = edge[e].v;
if(!dfn[v])
{
dfs(v);
low[u] = min(low[u], low[v]);
}
else if(ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u])
{
scnt++;
do
{
v = stack[--top];
belong[v] = scnt;
ins[v] = 0;
}while(v != u);
}
}
inline void Tarjan()
{
for(int v = 1; v <= n; v++) if(!dfn[v])
dfs(v);
}
inline void solve()
{
Tarjan();
for(int u = 1; u <= n; u++)
{
for(int e = first[u]; e != -1; e = edge[e].next)
{
int v = edge[e].v;
if(belong[u] != belong[v])
{
ind[belong[v]]++;
}
}
}
int SCC = 0;
for(int i = 1; i <= scnt; i++)
{
if(ind[i] == 0) SCC++;
cost[i] = INF;
}
for(int i = 1; i <= n; i++)
{
if(ind[belong[i]] == 0)
{
cost[belong[i]] = min(cost[belong[i]], money[i]);
}
}
for(int i = 1; i <= scnt; i++) if(cost[i] != INF)
{
ans += cost[i];
}
printf("%d %d\n", SCC, ans);
}
int main()
{
while(~scanf("%d%d", &n, &m))
{
init();
read_graph2();
solve();
}
return 0;
}