1. 程式人生 > 其它 >AcWing 1135. 新年好

AcWing 1135. 新年好

目錄

題目傳送門

題目描述

重慶城裡有 nn 個車站,mm 條 雙向 公路連線其中的某些車站。

每兩個車站最多用一條公路連線,從任何一個車站出發都可以經過一條或者多條公路到達其他車站,但不同的路徑需要花費的時間可能不同。

在一條路徑上花費的時間等於路徑上所有公路需要的時間之和。

佳佳的家在車站 11,他有五個親戚,分別住在車站 a,b,c,d,ea,b,c,d,e。

過年了,他需要從自己的家出發,拜訪每個親戚(順序任意),給他們送去節日的祝福。

怎樣走,才需要最少的時間?

輸入格式

第一行:包含兩個整數 n,mn,m,分別表示車站數目和公路數目。

第二行:包含五個整數 a,b,c,d,ea,b,c,d,e,分別表示五個親戚所在車站編號。

以下 mm 行,每行三個整數 x,y,tx,y,t,表示公路連線的兩個車站編號和時間。

輸出格式

輸出僅一行,包含一個整數 TT,表示最少的總時間。

資料範圍

1≤n≤500001≤n≤50000,
1≤m≤1051≤m≤105,
1<a,b,c,d,e≤n1<a,b,c,d,e≤n,
1≤x,y≤n1≤x,y≤n,
1≤t≤1001≤t≤100

輸入樣例:

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

輸出樣例:

21

多次Dijkstra + dfs

分析

首先使用6次Dijkstra,找到從 1,a,b,c,d,e六個點到其他所有點的最短路徑,存到表裡面,dist[i][j]表示的就是從i點出發,到j點的最短路徑,由於i只有六個,所以開dist的時候只需要開成dist[6][N]

然後dfs列舉 a,b,c,e,d訪問的順序,找到最短距離的訪問順序

  • 學習這裡dfs的方法,自己想想有沒有別的寫該dfs的方法?

程式碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 50010;
const int INF  = 0x3f3f3f3f;
int n,m;
int source[6];
int dist[6][N]; // 要做6次dijkstra 
bool st[N]; //  
 
struct VER
{
	int to, w;
};
vector<VER> h[N];
void add(int a, int b, int w)
{
	VER ver;
	ver.to = b;
	ver.w = w;
	h[a].push_back(ver);
}

void dijkstra(int start, int dis[])
{
	memset(dis, 0x3f, N * 4);// 把dist[start][N]都搞成正無窮 
	dis[start] = 0; 
	memset(st, 0, sizeof st); // 注意這裡每次要重置 

	priority_queue<PII, vector<PII>, greater<PII> > heap; // 小根堆 
	heap.push({0, start});
	
	while(heap.size())
	{
		PII tmp = heap.top();
		heap.pop();
		
		int  ver = tmp.second;
		
		if(st[ver]) continue; // 
		st[ver] = true; 
		
		for(int i = 0; i < h[ver].size(); i ++)
		{
			int j = h[ver][i].to;
			int w = h[ver][i].w;
			
			if(dis[j] > dis[ver] + w)
			{
				dis[j] = dis[ver] + w;
				heap.push({dis[j], j});
			}
		}
		
	}
}
//拜訪u個人(1號是第一個),從source[start]開始,最短距離是distance 
int dfs(int u, int start, int distance)
{
	if(u > 5) return distance; // 第六個人時,返回最短距離 
	
	int res = INF;
	for(int i = 1; i <= 5; i++)
	{
		if(!st[i])
		{
			st[i] = true;
			int t = source[i]; // 從start到t的最短距離 
			res = min(res,  dfs(u+1, i, distance + dist[start][t]));
			st[i] = false;
		}
	}
	
	return res;
}

int main()
{
	scanf("%d%d", &n, &m);
	source[0] = 1;
	for(int i = 1; i <= 5; i++) scanf("%d", &source[i]);
	
	while(m --)
	{
		int a, b, w;
		scanf("%d%d%d", &a, &b, &w);
		add(a, b, w);
		add(b, a, w);
	}
	
	// 對所有source[i] 做一次dijkstra,得到從該點出發到其他所有點的最短距離
	 
	for(int i = 0; i < 6; i ++)  dijkstra(source[i], dist[i]);
	
	
	memset(st, 0, sizeof st);
	int r = dfs(1, 0, 0);
	printf("%d\n", r);
	return 0;
}

時間複雜度

參考文章

https://www.acwing.com/solution/content/42071/

include<bits/stdc++.h>

define x first

define y second

using namespace std;
const int N = 50010, M = 2e5 +10;
const int INF = 0x3f3f3f3f;
typedef pair<int ,int > PII;

int n, m;
int source[6];
bool st[N];

int dist[6][N];
int h[N],e[M],ne[M],w[M],idx;

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

void dijkstra(int start, int dist[]){
memset(dist, 0x3f, 4N); // int 4個位元組,所以大小是4N
memset(st,0, sizeof st);
dist[start] = 0;
priority_queue<PII,vector, greater> heap;

heap.push({0,start}); // 距離,點

while(heap.size()){
    auto t = heap.top();
    heap.pop();
    int ver = t.y, distance = t.x;
    if(st[ver]) continue;
    st[ver] = true;

    for(int i = h[ver]; ~i; i = ne[i]){
        int j = e[i];
        if(dist[j] > distance + w[i]){
            dist[j] = distance + w[i];
            heap.push({dist[j], j});
        }
    }
}

}

// 列舉每種拜訪次序,求出最小距離
// 拜訪了u個人,自己是第1個人;當前起點是source[start],當前走過的距離是distance
int dfs(int u, int start, int distance){
// u== 6表示:拜訪完5個親戚,此時返回最短路
if( u == 6) return distance;
// res存距離最短的分支
int res = INF;
for(int i = 1; i<=5; i ++)
if(!st[i]){
int next = source[i]; // 走親戚i
st[i] = true;
res = min(res, dfs(u +1, i, dist[start][next] + distance));
st[i] = false;
}
return res;
}

int main(){
cin >> n >> m;
memset(h, -1, sizeof h);
source[0] = 1;
for(int i = 1; i <= 5; i ++) cin >> source[i];

while(m --){
    int x, y, t;
    cin >> x >> y >> t;
    add(x, y, t), add(y, x, t);
}
// 6 個點,分別求最短路
for(int i =0; i < 6; i ++) dijkstra(source[i],dist[i]);
/*
1. 共有6個人,起點是自己:第1個人
2. 當前是source[0]:佳佳
3. 當前走過的距離是0
*/
 memset(st,0, sizeof st);
 printf("%d\n", dfs(1, 0, 0));

}

作者:shizhengLee
連結:https://www.acwing.com/solution/content/42071/
來源:AcWing
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

y總程式碼

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

using namespace std;

typedef pair<int, int> PII;

const int N = 50010, M = 200010, INF = 0x3f3f3f3f;

int n, m;
int h[N], e[M], w[M], ne[M], idx;
int q[N], dist[6][N];
int source[6];
bool st[N];

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

void dijkstra(int start, int dist[])
{
    memset(dist, 0x3f, N * 4);
    dist[start] = 0;
    memset(st, 0, sizeof st);

    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, start});

    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.second;
        if (st[ver]) continue;
        st[ver] = true;

        for (int i = h[ver]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[ver] + w[i])
            {
                dist[j] = dist[ver] + w[i];
                heap.push({dist[j], j});
            }
        }
    }
}

int dfs(int u, int start, int distance)
{
    if (u > 5) return distance;

    int res = INF;
    for (int i = 1; i <= 5; i ++ )
        if (!st[i])
        {
            int next = source[i];
            st[i] = true;
            res = min(res, dfs(u + 1, i, distance + dist[start][next]));
            st[i] = false;
        }

    return res;
}

int main()
{
    scanf("%d%d", &n, &m);
    source[0] = 1;
    for (int i = 1; i <= 5; i ++ ) scanf("%d", &source[i]);

    memset(h, -1, sizeof h);
    while (m -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c), add(b, a, c);
    }

    for (int i = 0; i < 6; i ++ ) dijkstra(source[i], dist[i]);

    memset(st, 0, sizeof st);
    printf("%d\n", dfs(1, 0, 0));

    return 0;
}

作者:yxc
連結:https://www.acwing.com/activity/content/code/content/141215/
來源:AcWing
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。