水题代码求条
  • 板块灌水区
  • 楼主BK小鹿
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/8/25 22:45
  • 上次更新2024/8/26 10:08:25
查看原帖
水题代码求条
1345783
BK小鹿楼主2024/8/25 22:45

rt,样例都没过,晚上脑子都不好使了。。。

题目

WA Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char *p1,*p2,buf[1<<20+5];
inline int read(){
    int x=0;
    char c=gc();
    while(!isdigit(c)){
        c=gc();
    }while(isdigit(c)){
        x=x*10+(c^48);
        c=gc();
    }
    return x;
}

const int N = 50005, M = 200005;

int n, m;
int a[15];
int h[N], e[M], ne[M], w[M], idx;
int res;
int dist[N][15];
bool st[N], z[15];

void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void spfa(int S)
{
	memset(dist, 0x3f, sizeof dist);
	memset(st, 0, sizeof st);
	queue<int> q;
	q.push(S);
	st[S] = 1;
	dist[S][S] = 0;
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		st[t] = 0;
		for (int i = h[t]; ~i; i = ne[i])
		{
			int j = e[i];
			if (dist[t][S] + w[i] < dist[j][S])
			{
				dist[j][S] = dist[t][S] + w[i];
				if (!st[j])
				{
					q.push(j);
					st[j] = 1;
				}
			}
		}
	}
}
void dfs(int s, int p, int c)
{
	if (s >= 5)
	{
		res = min(res, c);
		return;
	}
	for (int i = 1; i <= 5; i ++ )
	{
		if (z[i]) continue;
		z[i] = 1;
		dfs(s + 1, i, c + dist[a[i]][p]);
		z[i] = 0;
	}
}

int main()
{
	memset(h, -1, sizeof h);
	n = read(), m = read();
	for (int i = 1; i <= 5; i ++ ) a[i] = read();
	for (int i = 1; i <= m; i ++ )
	{
		int x, y, t;
		x = read(), y = read(), t = read();
		add(x, y, t);
		add(y, x, t);
	}
	a[6] = 1;
	spfa(6), spfa(1), spfa(2), spfa(3), spfa(4), spfa(5);
	for (int i = 1; i <= 5; i ++ )
	{
		z[i] = 1;
		dfs(1, i, dist[a[i]][6]);
		z[i] = 0;
	}
	printf("%d\n", res);
	return 0;
}
2024/8/25 22:45
加载中...