推荐
题意
给定一个n个顶点,m条边的无向图,每条边有颜色值(长度均为1),求出起点到终点的最短路径(颜色值表示),若存在多解,则输出路径中颜色值字典序最小者。(注意重边,自环)
思路
逆向BFS求最短路径,正向BFS求字典序最小
代码如下
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
typedef long long ll;
typedef pair<int,int> PII;
int n,m;
struct Edge
{
int from,to,color;
};
vector<Edge>edges;
vector<int>G[maxn];
void init()
{
for(int i=0;i<=n;i++)
G[i].clear();
edges.clear();
}
void add(int from,int to,int color)
{
edges.push_back({
from,to,color});
int m=edges.size();
G[from

最低0.47元/天 解锁文章
331

被折叠的 条评论
为什么被折叠?



