[题解] [JLOI2011]飞行路线
[JLOI2011]飞行路线 题解
题目描述
Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?
输入格式
数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。
第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t<n)
接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b<n,a与b不相等,0<=c<=1000)
输出格式
只有一行,包含一个整数,为最少花费。
样例
样例输入
5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
样例输出
8
数据范围与提示
对于30%的数据,2<=n<=50,1<=m<=300,k=0;
对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.
解析
10分做法:
爆搜删边,求出其最小解法。
#include <map>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 1e5 + 5;
const int MAXM = 5e5 + 5;
struct Node {
int v, m;
Node() {}
Node(int V, int M) {
v = V;
m = M;
}
friend bool operator < (Node x, Node y) {
return x.m > y.m;
}
};
priority_queue<Node> q;
map<pair<int, int>, int> mp;
map<pair<int, int>, bool> vis;
map<pair<int, int>, bool> vis1;
vector<Node> a[MAXN];
int d[MAXN], x[MAXM], y[MAXM];
bool f[MAXN];
int n, m, S, T, s, minn = 0x7f7f7f7f;
void Read();
void dfs(int, int);
int Dijkstra(int, int);
int main() {
Read();
dfs(1, 0);
cout << minn;
return 0;
}
void dfs(int now, int last) {
if(last == s)
minn = min(minn, Dijkstra(S, T));
if(now == m + 1)
return;
a[x[now]].push_back(Node(y[now], 0));
a[y[now]].push_back(Node(x[now], 0));
dfs(now + 1, last + 1);
a[x[now]].pop_back();
a[y[now]].pop_back();
dfs(now + 1, last);
}
void Read() {
scanf("%d %d %d %d %d", &n, &m, &s, &S, &T);
for(int i = 1; i <= m; i++) {
int A, B, C;
scanf("%d %d %d", &A, &B, &C);
x[i] = A; y[i] = B;
if(!vis[make_pair(A, B)]) {
mp[make_pair(A, B)] = C;
mp[make_pair(B, A)] = C;
vis[make_pair(A, B)] = 1;
vis[make_pair(B, A)] = 1;
}
else {
mp[make_pair(A, B)] = min(mp[make_pair(A, B)], C);
mp[make_pair(B, A)] = mp[make_pair(A, B)];
}
}
for(int i = 1; i <= m; i++) {
if(!vis1[make_pair(x[i], y[i])]) {
a[x[i]].push_back(Node(y[i], mp[make_pair(x[i], y[i])]));
a[y[i]].push_back(Node(x[i], mp[make_pair(x[i], y[i])]));
vis1[make_pair(x[i], y[i])] = 1;
vis1[make_pair(y[i], x[i])] = 1;
}
}
}
int Dijkstra(int c, int t) {
q.push(Node(c, 0));
memset(d, 0x3f, sizeof(d));
memset(f, 0, sizeof(f));
d[c] = 0;
while(!q.empty()) {
Node now = q.top();
q.pop();
int i = now.v;
if(f[i]) continue;
f[i] = 1;
int SIZ = a[i].size();
for(int j = 0; j < SIZ; j++) {
if(d[a[i][j].v] > d[i] + a[i][j].m) {
d[a[i][j].v] = d[i] + a[i][j].m;
q.push(Node(a[i][j].v, d[a[i][j].v]));
}
}
}
return d[t];
}
当然,这是考试时实在没想到正解而做出的无奈之举。
可以发现k<=10,范围极小。
所以本题就可以用到分层图:
由上图可知:复制k+1份图连接相邻两层的对应点,由题意得:每层图由长度为0的边相连,就可以等价于有k次机会能够免费。再跑一边最短路Dijkstra即可。
所以说这道题就是一道板题。
正解C++代码:
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN = 1e6 + 5;
const int MAXM = 1e6 + 5;
struct Node {
int v, m;
Node() {}
Node(int V, int M) {
v = V;
m = M;
}
friend bool operator < (Node x, Node y) {
return x.m > y.m;
}
};
priority_queue<Node> q;
vector<Node> a[MAXN];
int d[MAXM], x[MAXM], y[MAXM];
bool f[MAXN];
int n, m, S, T, k, minn = 0x7f7f7f7f;
void Read();
int Dijkstra(int, int);
int main() {
Read();
cout << Dijkstra(S, T + k * n);
return 0;
}
void Read() {
scanf("%d %d %d %d %d", &n, &m, &k, &S, &T);
for(int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
a[u].push_back(Node(v, w));
a[v].push_back(Node(u, w));
for(int j = 1; j <= k; j++) {
a[u + (j - 1) * n].push_back(Node(v + j * n, 0));
a[v + (j - 1) * n].push_back(Node(u + j * n, 0));
a[u + j * n].push_back(Node(v + j * n, w));
a[v + j * n].push_back(Node(u + j * n, w));
}
}
for(int i = 1; i <= k; i++)
a[T + (i - 1) * n].push_back(Node(T + i * n, 0));
}
int Dijkstra(int c, int t) {
q.push(Node(c, 0));
memset(d, 0x7f, sizeof(d));
memset(f, 0, sizeof(f));
d[c] = 0;
while(!q.empty()) {
Node now = q.top();
q.pop();
int i = now.v;
if(f[i]) continue;
f[i] = 1;
int SIZ = a[i].size();
for(int j = 0; j < SIZ; j++) {
if(d[a[i][j].v] > d[i] + a[i][j].m) {
d[a[i][j].v] = d[i] + a[i][j].m;
q.push(Node(a[i][j].v, d[a[i][j].v]));
}
}
}
return d[t];
}