概念

在图论中,比如最短路、网络流等,题目会有一些额外的对边的权值的操作。比如可以将一定数量的边权减半或者变为0,然后在此基础上求最优解。
下图中,黑色的边相当于正常的建边,蓝色的边相当于一次特殊的权值操作(减半或者变0)。从一个点到另一个点,如果选择特殊操作,就从这一层到达下一层。
在这里插入图片描述建边操作的代码:空间复杂度O(mk)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cin>>n>>m>>k;
while(m--)
{
int a, b, c; cin>>a>>b>>c;
add(a, b, c);
add(b, a, c);
for(int j = 1; j <= k; j++)
{
// j层和j+1层之间建边,0代表特殊操作为 边的权值变为0
add(a+(j-1)*n, b+j*n, 0);
add(b+(j-1)*n, a+j*n, 0);

// j+1层建边
add(a+j*n, b+j*n, c);
add(b+j*n, a+j*n, c);
}
}

应用

acwing 340通信线路

题意
在这里插入图片描述

题解

建分层图,每两层之间建的边权为0,然后跑最短路。但题目最终要求的是路径上最大值是多少,所以dis的转移方程要变为

1
2
3
4
5
if(dis[v] > max(dis[u], e[i].val))
{
dis[v] = max(dis[u], e[i].val);
q.push(node{v, dis[v]});
}

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define endl "\n"
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define debug(a) cout<<"\tdebug:"<<a<<endl
#define for1(i, a, b) for(int i = a; i <= b; i++)
#define for2(i, b, a) for(int i = b; i >= a; i--)
const double PI = acos(-1.0);
const int mod = 998244353;
const int eps = 1e-8;
const int N = 10 + 1e6;

void slove();
template<typename T>void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-') f *= -1; ch = getchar();}
while(isdigit(ch)){x = x*10+ch-48; ch = getchar();} x *= f;
}
template<typename T>void print(T x)
{
if(x < 0) putchar('-'), x = -x;
if(x >= 10) print(x/10);
putchar(x % 10 + '0');
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
IOS;
slove();
return 0;
}

int n, m, tot, k, s, t;
int vis[N], head[N], dis[N];

struct edge
{
int to, next, val;
}e[N];

struct node
{
int to, val;
bool operator < (const node&b) const
{
return val > b.val;
}
};

void add(int u, int v, int val)
{
e[tot].to = v; e[tot].val = val; e[tot].next = head[u]; head[u] = tot++;
}

void dijkstra(int s)
{
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;

priority_queue<node> q;
q.push(node{s, 0});

while(!q.empty())
{
node x = q.top(); q.pop();
int u = x.to;
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if(dis[v] > max(dis[u], e[i].val))
{
dis[v] = max(dis[u], e[i].val);
q.push(node{v, dis[v]});
}
}
}
}


void slove()
{
memset(head, -1, sizeof head);
cin>>n>>m>>k;
s = 1; t = n;
while(m--)
{
int a, b, c; cin>>a>>b>>c;
add(a, b, c);
add(b, a, c);
for(int j = 1; j <= k; j++)
{
// j层和j+1层之间建边,0代表特殊操作为 边值变为0
add(a+(j-1)*n, b+j*n, 0);
add(b+(j-1)*n, a+j*n, 0);

// j+1层建边
add(a+j*n, b+j*n, c);
add(b+j*n, a+j*n, c);
}
}
// 加上这一段代码就代表着最终答案答案一定在k+1层图的第n个点也就是dis[(k + 1) * n]
// 当然也可以不加这一段,在每一层的第n个点找最小的值
// for(int i = 0; i <= k; i++) ans = min(ans, dis[n+i*n]);

// 要防止从1到n存在一条路的边数小于k !!!!!!!
for(int i = 1; i <= k; i++) add(i*n, (i+1)*n, 0);
dijkstra(s);
if(dis[(k+1)*n] == inf) cout<<"-1"<<endl;
else cout<<dis[(k+1)*n]<<endl;
}