当前位置:网站首页>Ideal Path(UVA - 1599)
Ideal Path(UVA - 1599)
2022-07-26 01:23:00 【zjsru_ Beginner】
author :Alex_Dawn
Title Description
Given a nn A little bit mm Undirected graph of strip and edge , Each side is painted 1 Color . Find the point 11 point-to-point nn A path for , To minimize the number of sides to pass , Under this premise , The color sequence passing through the edge is the smallest . There may be self rings and double edges . Input ensures that at least one connection exists 11 and nn The path of .
I/o sample
Input
4 6
1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1
Output
2
1 3
Ideas
This question has been used twice bfs, From the end bfs Record the shortest distance from each node to the end , From the beginning bfs Record the minimum order of the color dictionary , Data uses adjacency table to store the number of graph storage edges , At the same time, use the array subscript to represent the number of edges
Be careful
about bfs Tags accessed
From the beginning bfs For the first time bfs Result
Pay attention to self ring and double edge problem
AC Code
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000; // Maximum number of vertices
struct Edge { // edge
int u, v, c;
Edge(int _u, int _v, int _c) : u(_u), v(_v), c(_c) {} // Default constructor
};
vector<Edge> edge; // Edge cache , Array subscript as number
vector<int> G[maxn]; // Adjacency table to store graph , The edges are numbered ;
int n, m, a, b, c;
int d[maxn], vis[maxn]; // d: The distance from the end point to each point ( level );vis: Access array
void revBfs() { // Traverse from the end , Calculate the distance to each point
memset(d, 0, sizeof(d)); memset(vis, 0, sizeof(vis)); // initialization
queue<int> q;
q.push(n-1); vis[n-1] = 1; // Finish line up
while (!q.empty()) {
int u = q.front(); q.pop(); // The top team
for (int e : G[u]) { // Each adjacent side
int v = (u == edge[e].u) ? edge[e].v : edge[e].u;// similar if Judge , Ternary expression
if (vis[v] == 0) { // Not accessed
d[v] = d[u] + 1; // distance / Level update
q.push(v);
vis[v] = 1; // Tag access
}
}
}
}
void bfs() { // From the beginning bfs, Record the path with the smallest dictionary order
printf("%d\n", d[0]); // Shortest distance
memset(vis, 0, sizeof(vis)); // initialization
vector<int> next{0}, ans;
for (int i=0; i < d[0]; i ++) { // Hierarchical traversal
int minColor=0x3fffffff; // Minimum color value , Is an infinite constant
for (int u : next) // The vertex of this layer ( Find out the minimum color value from this layer to the next layer )
for (int e : G[u]) { // All sides
int v = (u == edge[e].u) ? edge[e].v : edge[e].u; // Determine the next vertex
if (d[u] == d[v]+1 && edge[e].c < minColor) minColor = edge[e].c; // Find the smallest color edge that can reach the end
}
ans.push_back(minColor); // Store the minimum color
vector<int> tnext; // Temporary storage
for (int u : next) // Add the point with the minimum color value from this layer to the next layer to the next round next
for (int e : G[u]) {
int v = (u == edge[e].u) ? edge[e].v : edge[e].u;
if (d[u] == d[v]+1 && vis[v] == 0 && edge[e].c == minColor) {
tnext.push_back(v);
vis[v] = 1;
}
}
next = tnext; // to update next Array
}
for (int i=0; i < ans.size(); i ++) printf("%d%s", ans[i], i == ans.size()-1 ? "\n" : " ");
}
int main() {
while (scanf("%d %d", &n, &m) == 2) {
edge.clear(); fill(G, G+maxn, vector<int>{}); // initialization
while (m --) {
scanf("%d %d %d", &a, &b, &c);
if (a == b) continue; // Deal with self loop ,continue jump
G[a-1].push_back(edge.size()); // Undirected graph , from 0 Start storage
G[b-1].push_back(edge.size());
edge.push_back({a-1,b-1,c}); // Edge cache
}
revBfs();
bfs();
}
return 0;
}边栏推荐
- What is the dynamic IP address? Why do you recommend dynamic IP proxy?
- Lua basic grammar
- Small sample learning data set
- Tencent employees' salary: the real 985 graduation salary, do you think I can be saved? Netizen: daily salary?
- 加载dll失败
- Failed to load DLL
- U++ learning notes ustruct, uenum declaration and function library simple function implementation
- 【Code】剑指offer 03数组中重复的数字
- Leetcode537. 复数乘法(可以,已解决)
- Format JS code and debug JS code
猜你喜欢

Google gson usage details

What is informatization? What is digitalization? What are the connections and differences between the two?

PyCharm在创建py文件时自动添加头部注释

Browser development and use skills

MDK compilation process and arm compilation tool chain

Android SQLite first groups and then sorts left concatenated queries

REST-assured接口测试框架详解

RHCE之at和crontab命令详解及chrony部署

谷歌浏览器调试工具使用基础版(一)

Docker高级篇-Mysql主从复制
随机推荐
Detailed explanation of at and crontab commands of RHCE and deployment of Chrony
matlab 移位操作基础
Iftnews | suppose this is what the metauniverse looks like 20 years later
Four common simple and effective methods for changing IP addresses
《分布式微服务电商》专题(一)-项目简介
B - Krypton Factor(dfs+回溯法)
腾讯员工晒出薪资:真实985毕业薪资,大家看我还有救吗?网友:日薪?
Gcdqueue encapsulation
全国一半人跑长沙,长沙一半人跑哪?
Docker advanced -mysql master-slave replication
[ickim 2022] the Fourth International Conference on knowledge and information management
Working principle of ZK rollups
Leetcode 537. 复数乘法(网友思路,自愧不如)
What if win11 cannot open its own anti-virus software? Win11's built-in anti-virus function cannot be turned on
FreeBSD bNXT Ethernet driver source code reading record 2:
RHCE之at和crontab命令详解及chrony部署
【数据挖掘】生成模型和判别模型的区别及优缺点
格式化JS代码,调试JS代码
Arthas watch 命令查看数组中对象的属性
Browser development and use skills