NOIP模拟(10.26)T2 做运动

做运动

题目背景:

10.26 NOIP模拟T2

分析:并查集 + 最短路dijkstra

 

考场上卡常数卡到想打人,出了点稍微构造过的数据······然后在本机上1.1s ~ 1.2s死都卡不进1s,然而,貌似评测姬还是比较给力的,我还是过掉了,虽然有两个点卡的不行······直接说标算吧,首先因为第一优先的条件是最高温度最低,那么我们将所有的边按照温度从小到大排序,然后依次加入,然后并查集维护连通性,直到st在同一个连通块,将温度小于等于这个温度的所有边加入,然后用这些加入的边跑一发最短路就好了。不过真的卡常到死啊,不开O2T飞飞啊·······

Source

/*
	created by scarlyw
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <ctime>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static bool iosig;
	static char c;
	for (iosig = false, c = read(); !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;
	}
	for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;

inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static bool iosig;
	static char c;
	for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;
	}
	for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 500000 + 10;
const int MAXM = 1000000 + 10;
const long long INF = 1000000010000000000;

int n, m, x, s, t;
struct edges {
	int from, to, t;
	long long w;
	inline bool operator < (const edges &a) const {
		return t < a.t;
	}
} e[MAXM];

struct node {
	int to;
	long long w;
	node(int to = 0, long long w = 0) : to(to), w(w) {};
	inline bool operator < (const node &a) const {
		return w > a.w;
	}
} ;

std::vector<node> edge[MAXN];

inline void add_edge(int x, int y, long long w) {
	edge[x].push_back(node(y, w)), edge[y].push_back(node(x, w));
}

int father[MAXN], rk[MAXN];

inline int get_father(int x) {
	return (father[x] == x) ? (x) : (father[x] = get_father(father[x]));
}

inline void unite(int x, int y) {
	int fa1 = get_father(x), fa2 = get_father(y);
	if (fa1 != fa2) {
		(rk[fa1] == rk[fa2]) ? (father[fa1] = fa2, rk[fa2]++) : (rk[fa1] < 
			rk[fa2] ? father[fa1] = fa2 : father[fa2] = fa1);
	}
}

inline void read_in() {
	R(n), R(m);
	for (int i = 1; i <= n; ++i) father[i] = i, rk[i] = 0;
	for (int i = 1; i <= m; ++i) {
		R(e[i].from), R(e[i].to), R(e[i].t);
		R(x), e[i].w = (long long)x * (long long)e[i].t;
	}
	R(s), R(t);
}

inline void solve() {
	int ans;
	std::sort(e + 1, e + m + 1);
	for (register int i = 1; i <= m; ++i) {
		unite(e[i].from, e[i].to), add_edge(e[i].from, e[i].to, e[i].w);
		if (get_father(s) == get_father(t)) {
			ans = e[i].t;
			while (i < m && e[i + 1].t == e[i].t) 
				++i, add_edge(e[i].from, e[i].to, e[i].w);
			break ;
		}
	}
	static long long dis[MAXN];
	static bool vis[MAXN];
	std::priority_queue<node> q;
	for (register int i = 1; i <= n; ++i) dis[i] = INF;
	dis[s] = 0, q.push(node(s, 0));
	while (!q.empty()) {
		while (!q.empty() && vis[q.top().to]) q.pop();
		if (q.empty()) break ;
		int cur = q.top().to;
		if (cur == t) break ;
		q.pop(), vis[cur] = true;
		for (register int p = edge[cur].size() - 1; p >= 0; --p) {
			node *e = &edge[cur][p];
			if (!vis[e->to] && dis[e->to] > dis[cur] + e->w)
				dis[e->to] = dis[cur] + e->w, q.push(node(e->to, dis[e->to]));
		}
	}
	printf("%d %lld", ans, dis[t]);
}

int main() {
//	freopen("running.in", "r", stdin);
//	freopen("running.out", "w", stdout);
	read_in();
	solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值