2753: [SCOI2012]滑雪与时间胶囊
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 2274 Solved: 793
[ Submit][ Status][ Discuss]
Description
a180285非常喜欢滑雪。他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道
之间的交点(同时也是景点),而且每个景点都有一编号i(1<=i<=N)和一高度Hi。a180285
能从景点i 滑到景点j 当且仅当存在一条i 和j 之间的边,且i 的高度不小于j。
与其他滑雪爱好者不同,a180285喜欢用最短的滑行路径去访问尽量多的景点。如果仅
仅访问一条路径上的景点,他会觉得数量太少。于是a180285拿出了他随身携带的时间胶囊。
这是一种很神奇的药物,吃下之后可以立即回到上个经过的景点(不用移动也不被认为是
a180285 滑行的距离)。请注意,这种神奇的药物是可以连续食用的,即能够回到较长时间
之前到过的景点(比如上上个经过的景点和上上上个经过的景点)。
现在,a180285站在1号景点望着山下的目标,心潮澎湃。他十分想知道在不考虑时间
胶囊消耗的情况下,以最短滑行距离滑到尽量多的景点的方案(即满足经过景点数最大的前
提下使得滑行总距离最小)。你能帮他求出最短距离和景点数吗?
Input
输入的第一行是两个整数N,M。
接下来1行有N个整数Hi,分别表示每个景点的高度。
接下来M行,表示各个景点之间轨道分布的情况。每行3个整数,Ui,Vi,Ki。表示
编号为Ui的景点和编号为Vi的景点之间有一条长度为Ki的轨道。
Output
输出一行,表示a180285最多能到达多少个景点,以及此时最短的滑行距离总和。
Sample Input
3 3
3 2 1
1 2 1
2 3 1
1 3 10
Sample Output
3 2
HINT
【数据范围】
对于30%的数据,保证 1<=N<=2000
对于100%的数据,保证 1<=N<=100000
对于所有的数据,保证 1<=M<=1000000,1<=Hi<=1000000000,1<=Ki<=1000000000。
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 1E5 + 10;
const int T = 20;
typedef long long LL;
int n,m,tot,h[maxn],fa[maxn];
bool bo[maxn];
LL ans;
struct E{
int x,y,w; E(){}
E(int x,int y,int w): x(x),y(y),w(w){}
bool operator < (const E &b) const
{
if (h[y] > h[b.y]) return 1;
if (h[y] < h[b.y]) return 0;
return w < b.w;
}
}edgs[maxn*20];
vector <int> v[maxn];
queue <int> Q;
int getfa(int x) {return x == fa[x]?x:fa[x] = getfa(fa[x]);}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint(); m = getint();
for (int i = 1; i <= n; i++)
fa[i] = i,h[i] = getint();
for (int i = 1; i <= m; i++)
{
int x = getint(),y,w;
y = getint(); w = getint();
if (h[x] >= h[y]) v[x].push_back(y),edgs[++tot] = E(x,y,w);
if (h[y] >= h[x]) v[y].push_back(x),edgs[++tot] = E(y,x,w);
}
bo[1] = 1; Q.push(1);
while (!Q.empty())
{
int k = Q.front(); Q.pop();
for (int i = 0; i < v[k].size(); i++)
{
int to = v[k][i];
if (bo[to]) continue;
bo[to] = 1; Q.push(to);
}
}
int cnt = 0;
for (int i = 1; i <= n; i++) if (bo[i]) ++cnt;
sort(edgs + 1,edgs + tot + 1);
for (int i = 1; i <= tot; i++)
{
E e = edgs[i];
if (!bo[e.x] || !bo[e.y]) continue;
int fx = getfa(e.x),fy = getfa(e.y);
if (fx == fy) continue;
fa[fx] = fy; ans += 1LL*e.w;
}
cout << cnt << ' ' << ans;
return 0;
}