样例都过不了!求助!(非常急)
查看原帖
样例都过不了!求助!(非常急)
494699
卷王慢即快楼主2022/12/19 20:20
#include <bits/stdc++.h>
using namespace std;
int n, m;
int x, tmp = 0, sum = 0, cnt = 1, ans = 1e8;
int vis[20], len[20]; //已经访问过的点,点与初始点的距离 
int vec[20]; //过这个点能到达的点数 
int sep[20][20]; //每个点能到达的点 
int cost[20][20]; //花费 
inline int read()
{
	int 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 ^ 48);
		ch = getchar();
	}
	return x * f;
}
inline bool cmp(int a, int b)
{
	return cost[x][a] < cost[x][b]; //给每个点能到达的点进行排序 
}
inline void work(int now, int t)
{
	for(int i = now; i <= cnt; i++)
	{
		int u = vis[i];
		if(sum + tmp * len[u] >= ans) return ;
		for(int v = t; v <= vec[u]; v++)
			if(len[sep[u][v]])
			{
				vis[++cnt] = sep[u][v];
				sum += cost[u][vis[cnt]] * len[u];
				tmp -= cost[vis[cnt]][sep[u][1]];
				len[vis[cnt]] = len[u] + 1;
				work(i, v + 1);
				sum -= cost[u][vis[cnt]] * len[u];
				tmp += cost[vis[cnt]][sep[u][1]];
				len[vis[cnt--]] = 0;
			}
		t = 1;
	}
	if(cnt == n) ans = min(ans, sum);
}
int main()
{
	n = read(), m = read();
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			cost[i][j] = 1e8;
	for(int i = 1; i <= m; i++)
	{
		int u = read(), v = read(), w = read();
		if(cost[u][v] < w) continue;
		if(cost[u][v] == 1e8)
		{
			sep[u][++vec[u]] = v;
			sep[v][++vec[v]] = u;
		}
		cost[u][v] = w;
		cost[v][u] = w;
	}
	for(int i = 1; i <= n; i++)
	{
		x = i;
		sort(sep[i] + 1, sep[i] + 1 + vec[i], cmp);
		tmp += cost[i][sep[i][1]];
	}
	for(int i = 1; i <= n; i++)
	{
		sum = 0, cnt = 1;
		vis[1] = i;
		tmp -= cost[i][sep[i][1]];
		len[i] = 1;
		work(1, 1);
		tmp += cost[i][sep[i][1]];
		len[i] = 0;
	}
	printf("%d", ans);
	return 0;
}

两个样例都输出了 100000000100000000,本人查出 workwork 函数最后的判断一次都没有执行。

求助!求助!求求助!!!

2022/12/19 20:20
加载中...