第一个问题在于枚举
先看题目:
重庆城里有 n个车站,m 条 双向 公路连接其中的某些车站。
每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费的时间可能不同。
在一条路径上花费的时间等于路径上所有公路需要的时间之和。
佳佳的家在车站 11,他有五个亲戚,分别住在车站 a,b,c,d,e。
过年了,他需要从自己的家出发,拜访每个亲戚(顺序任意),给他们送去节日的祝福。
怎样走,才需要最少的时间?
第一行:包含两个整数 n,m,分别表示车站数目和公路数目。
第二行:包含五个整数 a,b,c,d,e,分别表示五个亲戚所在车站编号。
以下 m 行,每行三个整数 x,y,t,表示公路连接的两个车站编号和时间。
输出仅一行,包含一个整数 T,表示最少的总时间。
1≤n≤50000,
1≤m≤10^5,
1 1≤x,y≤n,
1≤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
代码如下
- #include
- #include
- #include
- #include
-
- using namespace std;
-
- typedef pair<int, int> PII;
- const int N = 5e4 + 10, M = 2e5 + 10, INF = 0x3f3f3f3f;
-
- int n, m;
- int h[N], w[M], e[M], ne[M], idx;
- int source[N];
- int dist[6][N];
- 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);//因为这边传过来的数组,实际是指针,所以是N*4
- memset(st, 0, sizeof st);
-
- dist[start] = 0;
-
- priority_queue
, greater> 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 != -1; 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});
- }
- }
- }
- }
-
- // 枚举每种拜访次序,求出最小距离
- // 拜访了u个人,自己是第1个人;当前起点是source[start],当前走过的距离是distance
- int dfs(int u, int start, int distance)
- {
- if(u > 5) return distance;//走完了,返回
-
- // res存距离最短的分支
- int res = INF;
- for(int i = 1; i < 6; 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()
- {
- cin >> n >> m;
-
- memset(h, -1, sizeof h);
-
- source[0] = 1;//第一个结点为1
- for(int i = 1;i <= 5; i ++) cin >> source[i];//输入剩余五个亲戚的站台号
-
- 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);//重置st数组,因为要再次使用
-
- cout << dfs(1, 0, 0) << endl;//输出答案
-
- return 0;
- }
第二个题目:
博主用的分层图做法
先看题目:
在郊区有 N 座通信基站,P条 双向 电缆,第 i 条电缆连接基站 Ai 和 Bi。
特别地,11 号基站是通信公司的总站,N 号基站位于一座农场中。
现在,农场主希望对通信线路进行升级,其中升级第 i 条电缆需要花费 Li。
电话公司正在举行优惠活动。
农产主可以指定一条从 11 号基站到 N 号基站的路径,并指定路径上不超过 K 条电缆,由电话公司免费提供升级服务。
农场主只需要支付在该路径上剩余的电缆中,升级价格最贵的那条电缆的花费即可。
求至少用多少钱可以完成升级。
第 11 行:三个整数 N,P,K。
第 2..P+1 行:第 i+1 行包含三个整数 Ai,Bi,Li。
包含一个整数表示最少花费。
若 11 号基站与 N 号基站之间不存在路径,则输出 −1−1。
0≤K
1≤Li≤1000000
代码如下:
- #include
- #include
- #include
- #include
-
- using namespace std;
-
- typedef pair<int, int> PII;
- //因为建议k层分层图,所以数据是这样的
- const int N = 1e6 + 10, M = 2e7 + 10, INF = 0x3f3f3f3f;
-
- int n, m, k;
- int h[N], w[M], e[M], ne[M], idx;
- int dist[N];
- 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()
- {
- memset(dist, 0x3f, sizeof dist);
-
- dist[1] = 0;
-
- priority_queue
, greater> heap; - heap.push({0, 1});
-
- 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], x = max(dist[ver], w[i]);//这里是比较价格最大的那个
- if(dist[j] > x)
- {
- dist[j] = x;
- heap.push({dist[j], j});
- }
- }
- }
- }
-
- int main()
- {
- cin >> n >> m >> k;
-
- 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 j = 1; j <= k; j ++)//k次免费,建议k层分层图
- {
- add(j * n + a, j * n + b, c);
- add(j * n + b, j * n + a, c);
- add((j - 1) * n + a, j * n + b, 0);
- add((j - 1) * n + b, j * n + a, 0);
- }
- }
-
- //给每层终点建一条边是为了防止出现用不到k次就出现最短路的情况
- for(int i = 1; i <= k; i ++) add(i * n, (i + 1) * n, 0);
-
- dijkstra();
-
- if(dist[(k + 1) * n] == INF) puts("-1");
- else cout <
1) * n] << endl; -
- return 0;
- }
明白的小伙伴麻烦点个赞吧