QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1277#799924#9352. Highway Busesfree_windyBINYUSuccess!2024-12-10 15:18:372024-12-10 15:18:38

Details

Extra Test:

Wrong Answer
time: 5ms
memory: 59108kb

input:

6 52 2
1 2349 -64
1 15856 -1270
5 16159 -134
2 21785 -1140
4 21248 -157
5 18103 -13992
1 2
2 3
2 4
3 5
5 6
4 2
3 6
5 1
2 6
4 6
5 4
4 3
1 6
3 2
5 6
5 6
2 4
2 4
1 5
3 6
2 4
1 6
6 4
2 1
5 4
4 6
3 4
6 2
2 5
6 4
6 5
6 2
2 5
1 4
6 1
6 1
2 6
4 1
4 5
4 3
4 5
6 3
2 5
6 2
3 6
3 1
6 5
5 4
2 3
2 6
2 6
1 4

output:

0
2285
2285
2285
2285
2349

result:

wrong answer 6th lines differ - expected: '2285', found: '2349'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#799924#9352. Highway BusesBINYUWA 1927ms378412kbC++145.6kb2024-12-05 19:36:122024-12-10 15:25:47

answer

#include<bits/stdc++.h>
using namespace std;
struct IO {
    static const int inSZ = 1 << 17;
    char inBuf[inSZ], *in1, *in2;
    template<class T> inline __attribute((always_inline))
    T read() {
        if (in1 > inBuf + inSZ - 32) [[unlikely]] {
            auto len = in2 - in1;
            memcpy(inBuf, in1, len);
            in1 = inBuf, in2 = inBuf + len;
            in2 += fread(in2, 1, inSZ - len, stdin);
            if (in2 != inBuf + inSZ) *in2 = 0;
        }
        T res = 0;
        unsigned char c;
        bool neg = 0;
        while ((c = *in1++) < 48) neg = c == 45;
        while (res = res * 10 + (c - 48), (c = *in1++) >= 48);
        return neg ? -res : res;
    }
    static const int outSZ = 1 << 21;
    char outBuf[outSZ], *out;
    template<class T> inline __attribute((always_inline))
    void write(T x) {
        if (out > outBuf + outSZ - 32) [[unlikely]]
        {
            fwrite(outBuf, 1, out - outBuf, stdout), out = outBuf;
		}
        if (!x) return *out++ = 48, void();
        if (x < 0) *out++ = 45, x = -x;
        alignas(2) const char* digits =
        "0001020304050607080910111213141516171819"
        "2021222324252627282930313233343536373839"
        "4041424344454647484950515253545556575859"
        "6061626364656667686970717273747576777879"
        "8081828384858687888990919293949596979899";
        alignas(64) static char buf[20];
        char* p = buf + 20;
        while (x >= 10) memcpy(p -= 2, digits + x % 100 * 2, 2), x /= 100;
        if (x) *--p = 48 + x;
        auto len = buf + 20 - p;
        memcpy(out, p, len), out += len;
    }
    void init() {
        in1 = in2 = inBuf + inSZ;
        out = outBuf;
    }
    ~IO() { fwrite(outBuf, 1, out - outBuf, stdout); }
} IO;
template<class T = int> inline T read() {
    return IO.read<T>();
}
template<class T> inline void print(T x, char c) {
    IO.write(x), *IO.out++ = c;
}
template<class T> inline void print(T x) {
    IO.write(x);
}
inline __attribute((always_inline)) void printCh(char c) {
    *IO.out++ = c;
}
#define pii pair <int,int>
#define pli pair <ll,int>
#define fi first
#define se second
#define ll long long
const int N = 2e5,M = 50;
int n,m,u,v,cnt,a[N + 5],b[N + 5],f[N + 5],vis[N + 5],siz[N + 5];
int Dis[N + 5],dis[M + 5][N + 5],t1[M + 5],t2[N + 5];
int T,c[N + 5],w[N + 5];
ll d[N + 5],ans[N + 5],C[N + 5];
priority_queue <pli,vector <pli>,greater <pli> > q;
queue <int> Q;
struct edge
{
	int v,nxt;
}e[2 * N + 5];
int head[N + 5],cnte;
struct Edge
{
	int v,nxt;
}E[2 * N + 2 * M + 5];
int Head[N + 5],cntE;
void adde(int u,int v)
{
	e[++cnte] = {v,head[u]};head[u] = cnte;
}
void addE(int u,int v)
{
	E[++cntE] = {v,Head[u]};Head[u] = cntE;
}
vector <pii> c2[N + 5];
pii c1[M + 5][N + 5];
unordered_map <int,int> mp[N + 5];
struct DSU
{
	int f[N + 5];
	void init(int n)
	{
		for(int i = 1;i <= n;i++)f[i] = i;
	}
	int fnd(int x)
	{
		return x == f[x] ? x : f[x] = fnd(f[x]);
	}
	bool merge(int x,int y)
	{
		x = fnd(x);y = fnd(y);
		if(x == y)return 0;
		f[x] = y;
		return 1;
	}
}dsu;
int get_siz(int u,int fa)
{
	siz[u] = 1;
	for(int i = head[u];i;i = e[i].nxt)
		if(!vis[e[i].v]&&e[i].v != fa)
			siz[u] += get_siz(e[i].v,u);
	return siz[u];
}
int get_root(int u,int fa,int Siz)
{
	int mx = Siz - siz[u],rt = -1;
	for(int i = head[u];i;i = e[i].nxt)
		if(!vis[e[i].v]&&e[i].v != fa)
			mx = max(mx,siz[e[i].v]),
			rt = max(rt,get_root(e[i].v,u,Siz));
	if(mx * 2 <= Siz)return u;
	return rt;
}
void init1(int st,int *dis,pii *c)
{
	Q.push(st);dis[st] = 0;int cnt = 0;
	while(!Q.empty())
	{
		int u = Q.front();Q.pop();
		c[cnt++] = {dis[u],u};
		for(int i = Head[u];i;i = E[i].nxt)
			if(E[i].v != st&&!dis[E[i].v]&&!vis[E[i].v])
				dis[E[i].v] = dis[u] + 1,Q.push(E[i].v);
	}
}
void init2(int st,int *dis,vector <pii> &c)
{
	Q.push(st);dis[st] = 0;
	while(!Q.empty())
	{
		int u = Q.front();Q.pop();
		c.push_back({dis[u],u});
		mp[st][u] = dis[u];
		for(int i = head[u];i;i = e[i].nxt)
			if(e[i].v != st&&!dis[e[i].v]&&!vis[e[i].v])
				dis[e[i].v] = dis[u] + 1,Q.push(e[i].v);
	}
	for(auto now : c)dis[now.second] = 0;
}
int solve(int u)
{
	vis[u] = 1;init2(u,Dis,c2[u]);
	for(int i = head[u];i;i = e[i].nxt)
		if(!vis[e[i].v])
			f[solve(get_root(e[i].v,u,get_siz(e[i].v,u)))] = u;
	return u;
}
void dij(ll x)
{
	for(int i = 1;i <= n;i++)
		C[i] = c[i] + w[i] * x,d[i] = vis[i] = t1[i] = t2[i] = 0;
	q.push({0,1});vis[1] = 1;
	while(!q.empty())
	{
		int u = q.top().second;q.pop();
		for(int i = 1;i <= cnt;i++)
		{
			int noww = b[u] - dis[i][u];
			while(t1[i] < n)
			{
				int v = c1[i][t1[i]].second;
				if(c1[i][t1[i]].first > noww)break;
				if(!vis[v])
					d[v] = d[u] + C[u],vis[v] = 1,
					q.push({d[v] + C[v],v});
				t1[i]++;
			}
		}
		int now = u;
		while(now)
		{
			int noww = b[u] - mp[now][u];
			while(t2[now] != c2[now].size())
			{
				int v = c2[now][t2[now]].second;
				if(c2[now][t2[now]].first > noww)break;
				if(!vis[v])
					d[v] = d[u] + C[u],vis[v] = 1,
					q.push({d[v] + C[v],v});
				t2[now]++;
			}
			now = f[now];
		}
	}
	for(int i = 1;i <= n;i++)ans[i] = min(ans[i],d[i]);
}
int main()
{
	IO.init();
	n = read();m = read();T = read();
	dsu.init(n);
	for(int i = 1;i <= n;i++)
		b[i] = read(),c[i] = read(),w[i] = read();
	for(int i = 1;i <= m;i++)
	{
		u = read(),v = read();
		addE(u,v);addE(v,u);
		if(dsu.merge(u,v))adde(u,v),adde(v,u);
		else a[++cnt] = u;
	}
	for(int i = 1;i <= cnt;i++)init1(a[i],dis[i],c1[i]);
	solve(get_root(1,0,get_siz(1,0)));
	memset(ans,0x3f,sizeof(ans));
	dij(0);dij(T - 1);
	for(int i = 1;i <= n;i++)print(ans[i],'\n');
}