题目链接
题目大意:
对于一个n个房间m条路径的迷宫(Labyrinth)(2<=n<=100000, 1<=m<=200000),每条路径上都涂有颜色,颜色取值范围为1<=c<=10^9。求从节点1到节点n的一条路径,使得经过的边尽量少,在这样的前提下,如果有多条路径边数均为最小,则颜色的字典序最小的路径获胜。一条路径可能连接两个相同的房间,一对房间之间可能有多条路径。输入保证可以从节点1到达节点n。
分析:
求理想路径的变式迷宫问题。
图的存储不能够在使用矩阵存储了,因为点的数目过大。可以使用邻接链表的方式存储:即一个点对应一个数组,数组中为与之相连的其他所有顶点。邻接链表可以使用结构体,但有些麻烦。我这里使用了vector+数组的形式:vector里面为顶点,对应数组即为其他顶点。
图的存储问题解决了。普通用bfs求迷宫问题变为记录父结点和到起点距离,然后从终点到起点遍历。但对本题不能使用,因为当有多条路径时,求的可能不是最短路径。
紫书上给了一种用两次bfs求最短路径的方法。第一次bfs:先从终点出发用dis数组记录终点到每个顶点的距离。第二次bfs:从起点出发,向下bfs,不过bfs入队的条件不再只是标记了,还要是下次的dis距离比上次小一,以及边的权值要为最小。所以第二次bfs先求出当前点到下一个点的最小权值,接着再判断入队。
一定要记得标记,因为本题会有回环,不标记会超时。又深刻学习了bfs求最短路径。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000+10;
const int maxc = 1000000000;
vector<int> node[maxn];
vector<int> color_node[maxn];
int dis[maxn], vis[maxn], res[maxn],n;
void re_bfs() {
queue<int> queue1;
queue1.push(n);
vis[n] = 1;
while(!queue1.empty()) {
int u = queue1.front(); queue1.pop();
for(int i = 0; i < node[u].size(); i++) {
int v = node[u][i];
if(!vis[v]) {
vis[v] = 1;
dis[v] = dis[u]+1;
queue1.push(v);
}
}
}
}
void bfs() {
memset(vis,0,sizeof(vis));
queue<int> queue1;
queue1.push(1);
vis[1] = 1;
while(!queue1.empty()) {
int mcolor = maxc;
int u = queue1.front(); queue1.pop();
for(int i = 0; i < node[u].size(); i++) {
int v = node[u][i];
if(dis[v]==dis[u]-1)
mcolor = min(mcolor, color_node[u][i]);
}
int cnt = dis[1]-dis[u];
if(!res[cnt]) res[cnt] = mcolor;
else res[cnt] = min(res[cnt],mcolor);
for(int i = 0; i < node[u].size(); i++) {
int v = node[u][i];
if(dis[v]==dis[u]-1 && !vis[v] && color_node[u][i]==mcolor) {
vis[v] = 1;
queue1.push(v);
}
}
}
}
int main() {
freopen("i.txt","r",stdin);
freopen("o.txt","w",stdout);
int m,x,y,color;
while(cin >> n >> m) {
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(res,0,sizeof(res));
for(int i = 1; i <= n; i++) node[i].clear();
for(int i = 1; i <= n; i++) color_node[i].clear();
while (m--) {
cin >> x >> y >> color;
if (x == y) continue;
node[x].push_back(y);
node[y].push_back(x);
color_node[x].push_back(color);
color_node[y].push_back(color);
}
re_bfs();
bfs();
cout << dis[1] << "\n" << res[0];
for(int i = 1; i < dis[1]; i++)
cout << " " << res[i];
cout << endl;
}
}
又用边集数组存储重新写了一遍,来熟练边集数组这种存储方法。当题目要以边展开题目时,可以使用这种存储方法。
除了存储方式不同,其他思路都相同。
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 10;
const int inf = (int)1e9+10;
int n,dis[maxn],vis[maxn],res[maxn];
struct Edge {
int u,v,c;//起点,终点,权值
Edge(int _u, int _v, int _c):
u(_u),v(_v),c(_c) {}
};
vector<Edge> edge;
vector<int> g[maxn];
void add(int uu, int vv, int cc) {
edge.emplace_back(uu,vv,cc);
g[uu].push_back(edge.size()-1);//存储第几条边
}
void re_bfs() {
queue<int> queue1;
queue1.push(n);
vis[n] = 1;
while(!queue1.empty()) {
int u = queue1.front(); queue1.pop();
for (int i : g[u]) {
int v = edge[i].v;
if(!vis[v]) {
vis[v] = 1;
dis[v] = dis[u]+1;
queue1.push(v);
}
}
}
}
void bfs() {
memset(vis,0,sizeof(vis));
queue<int> queue1;
queue1.push(1);
vis[1] = 1;
while(!queue1.empty()) {
int u = queue1.front(); queue1.pop();
int mcolor = inf;
for(int i : g[u]) {
int v = edge[i].v;
if(dis[v] == dis[u]-1)
mcolor = min(mcolor,edge[i].c);
}
int cnt = dis[1]-dis[u];
if(!res[cnt]) res[cnt] = mcolor;
else res[cnt] = min(mcolor,res[cnt]);
for(int i : g[u]) {
int v = edge[i].v;
if(dis[v] == dis[u]-1 && !vis[v] && edge[i].c==mcolor) {
vis[v] = 1;
queue1.push(v);
}
}
}
}
int main() {
freopen("i.txt","r",stdin);
freopen("o.txt","w",stdout);
int m,x,y,color;
while(cin >> n >> m) {
edge.clear();
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(res,0,sizeof(res));
for(int i = 1; i <= n; i++) g[i].clear();
while (m--) {
cin >> x >> y >> color;
if (x == y) continue;
add(x,y,color);
add(y,x,color);
}
re_bfs();
bfs();
cout << dis[1] << "\n" << res[0];
for(int i = 1; i < dis[1]; i++)
cout << " " << res[i];
cout << endl;
}
}