整理的算法模板:ACM算法模板總結(分類詳細版)
重慶城裏有 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
題解:
預處理每個親戚包括自己家到其他所有點的最短距離;然後dfs暴搜所有的狀態查表取最小值;
可以用dijkstra預處理最短距離;這道題數據比較毒瘤,會把spfa算法卡掉;但是加上SFA優化就沒問題了;
Dijkstra:
#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 root,int dist[])
{
memset(dist,0x3f,N*4);
dist[root] = 0;
memset(st, 0, sizeof st);
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, root});
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second,distance=t.first;
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});
}
}
}
}
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;
}
SPFA+SFL優化:
#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 spfa(int root,int dist[])
{
memset(dist,0x3f,N*4);
dist[root] = 0;
memset(st, false, sizeof st);
deque<int> q;
q.push_back(root);
st[root]=true;
while (q.size())
{
int res=q.front();
q.pop_front();
st[res]=false;
for(int i=h[res];i!=-1;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[res]+w[i])
{
dist[j]=dist[res]+w[i];
if(!st[j])
{
st[j]=true;
if(dist[j]>dist[q.front()]) q.push_back(j);
else q.push_front(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 ++ ) spfa(source[i],dist[i]);
memset(st, 0, sizeof st);
printf("%d\n", dfs(1, 0, 0));
return 0;
}