题目描述
To see a World in a Grain of Sand
And a Heaven in a Wild Flower,
Hold Infinity in the palm of your hand
And Eternity in an hour.
—— William Blake
听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间和电话费了。他知道其他人也有一些别人的联系方式,这样他可以通知其他人,再让其他人帮忙通知一下别人。你能帮Wiskey计算出至少要通知多少人,至少得花多少电话费就能让所有人都被通知到吗?
输入
多组测试数组,以EOF结束。
第一行两个整数N和M(1<=N<=1000, 1<=M<=2000),表示人数和联系对数。
接下一行有N个整数,表示Wiskey联系第i个人的电话费用。
接着有M行,每行有两个整数X,Y,表示X能联系到Y,但是不表示Y也能联系X。
输出
输出最小联系人数和最小花费。
每个CASE输出答案一行。
样例
Sample Input
12 16
2 2 2 2 2 2 2 2 2 2 2 2
1 3
3 2
2 1
3 4
2 4
3 5
5 4
4 6
6 4
7 4
7 12
7 8
8 7
8 9
10 9
11 10
Sample Output
3 6
题意
给定N个人,M条关系(N <=1000, M<= 2000),,如果a到b有边则表示第a个人能够联系到第b个人,联系第i个人的花费为v[i],联系具有传递性。现在需要联系到所有人,求最小联系人数和最小花费。
这个题写的头疼, 写出来一堆BUG 其实就是一个板子+判断一些东西而已
AC代码
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
#define ls st<<1
#define rs st<<1|1
#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int,int>
#define VI vector<int>
#define CLR(a,b) memset(a, (b), sizeof(a))
#define ALL(x) x.begin(),x.end()
#define rep(i,s,e) for(int i=(s); i<=(e); i++)
#define tep(i,s,e) for(int i=(s); i>=(e); i--)
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+10;
const int mod = 1e9+7;
const double eps = 1e-8;
void fe() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt","w",stdout);
#endif
}
LL read()
{
LL x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
stack<int> S;
vector<int> E[MAXN];
int n, m, x;
int in[MAXN], out[MAXN];
int dfn[MAXN], low[MAXN], bl[MAXN], cost[MAXN];
int cnt, tot, ans;
bool vis[MAXN];
void tarjan(int u) {
low[u] = dfn[u] = ++tot;
S.push(u);
vis[u] = true;
for(int i = 0; i < E[u].size(); i++) {
int v = E[u][i];
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u],low[v]);
}else if(vis[v]) {
low[u] = min(low[u],dfn[v]);
}
}
if(low[u] == dfn[u]) {
cnt++;
while(true) {
int v = S.top();
S.pop();
vis[v] = false;
bl[v] = cnt;
if(v == u) break;
}
}
}
void get_dag() {
for(int i = 1; i <= n; i++) {
for(int j = 0; j < E[i].size(); j++) {
int u = bl[i];
int v = bl[E[i][j]];
if(u != v) {
in[v]++;
}
}
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
while(cin >> n >> m) {
CLR(vis,false); CLR(low, 0); CLR(dfn, 0);
CLR(cost, 0); CLR(bl, 0); CLR(in, 0);
while(!S.empty()) S.pop();
for(int i = 1; i <= n; i++)
cin >> cost[i];
for(int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
E[a].push_back(b);
}
tot = 0, cnt = 0;
int k = 0;
ans = 0;
for(int i = 1; i <= n; i++)
if(!dfn[i])
tarjan(i);
get_dag();
for(int i = 1; i <= cnt; i++) {
if(!in[i]) {
k++;
int res = INF;
for(int j = 1; j <= n; j++) {
if(bl[j] == i)
res = min(res, cost[j]);
}
//cout << res << " ";
ans += res;
}
}
// cout << "\n";
cout << k << " " << ans << endl;
for(int i = 1; i <= n; i++) E[i].clear();
}
return 0;
}

本文探讨了一个社交网络通知问题,通过构建图模型并利用深度优先搜索(DFS)进行连通性分析,旨在找出最少的通知者数量及最低成本,确保消息能够覆盖整个社交网络。
2717

被折叠的 条评论
为什么被折叠?



