洛谷 P5764 [CQOI2005] 新年好

P5764 [CQOI2005] 新年好

链接:[CQOI2005] 新年好 - 洛谷

题目描述

重庆城里有 n 个车站,m 条双向公路连接其中的某些车站。每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费的时间可能不同。在一条路径上花费的时间等于路径上所有公路需要的时间之和。

佳佳的家在车站 1,他有五个亲戚,分别住在车站 a,b,c,d,e。过年了,他需要从自己的家出发,拜访每个亲戚(顺序任意),给他们送去节日的祝福。怎样走,才需要最少的时间?

输入格式

第一行:n,m,分别为车站数目和公路的数目。

第二行:a,b,c,d,e,分别为五个亲戚所在车站编号。

以下 m 行,每行三个整数 x,y,t,为公路连接的两个车站编号和时间。

输出格式

仅一行,包含一个整数 T,为最少的总时间。保证 T≤10的9次方。

输入输出样例

输入 #1

6 6
2 3 4 5 6
1 2 8
2 3 3
3 4 4
4 5 5
5 6 2
1 6 7

输出 #1

21
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

using namespace std;

const int N = 1e5+5;

int n,m;
int va[N];
int vb[N];
int vis[N];
int dist[6][N];

map<int,int> mp;
vector<PII > e[N];

void distra(int idx,int A)
{
	for(int i=1;i<=n;i++)
	{
		vis[i] = 0;
		dist[idx][i] = 1e18;
	}
	priority_queue<PII,vector<PII>,greater<PII>> q;
	q.push({0,A});
	dist[idx][A] = 0;
	while(q.size())
	{
		auto t = q.top();q.pop();
		int now = t.se;
		if(vis[now]) continue;
		vis[now] = 1;
		for(auto tt:e[now])
		{
			int spot = tt.se,w = tt.fi;
			if(dist[idx][spot]>dist[idx][now]+w)
			{
				dist[idx][spot] = dist[idx][now]+w;
				q.push({dist[idx][spot],spot});
			}
		}
	}
}

signed main()
{
	cin>>n>>m;
	for(int i=1;i<=5;i++) cin>>vb[i];
	while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		e[a].pb({c,b});
		e[b].pb({c,a});
	}
	vb[0] = 1;
	sort(vb+1,vb+1+5);
	for(int i=0;i<=5;i++)
	{
		distra(i,vb[i]);
		mp[vb[i]] = i;
	}
	int minn = 1e18;
	do{
		int sum = 0;
		for(int i=0;i<=4;i++)
		{
			sum += dist[mp[vb[i]]][vb[i+1]];
		}
		minn = min(minn,sum);
	}while(next_permutation(vb+1,vb+1+5));
	cout<<minn;
	return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值