It looks like you have a partial implementation of a graph traversal algorithm, possibly for finding the shortest path in a graph with some constraints. Let's complete the code and ensure it works correctly.
Here's the complete code:
#include <bits/stdc++.h>
#define maxn 1000
#define inf 1e9
#define mkp make_pair
#define pii pair<int,int>
#pragma GCC optimize(3,"Ofast","inline")
using namespace std;
struct node {
int a, b, t;
} edge[maxn];
struct p {
int num, t;
} dis[maxn];
int head[maxn], nxt[maxn], tot;
void add(int a, int b, int c) {
edge[++tot] = {a, b, c};
nxt[tot] = head[a];
head[a] = tot;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m;
cin >> m >> n;
string s;
getline(cin, s); // To consume the newline character after reading n
int x;
for (int j = 1; j <= m; j++) {
s = "";
getline(cin, s);
vector<int> v;
x = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
x *= 10;
x += s[i] - '0';
} else {
v.push_back(x);
x = 0;
}
}
if (x) v.push_back(x);
for (int i = 1; i < v.size(); i++) {
add(v[i - 1], v[i], j);
}
}
memset(dis, 0x3f, sizeof(dis));
queue<int> q;
q.push(1);
dis[1] = {0, 0};
while (!q.empty()) {
int num = q.front();
q.pop();
for (int i = head[num]; i; i = nxt[i]) {
int to = edge[i].b;
int t = edge[i].t;
if (dis[to].num > dis[num].num + 1 || (dis[to].num == dis[num].num + 1 && dis[to].t > dis[num].t + t)) {
dis[to] = {dis[num].num + 1, dis[num].t + t};
q.push(to);
}
}
}
if (dis[n].num == 0x3f3f3f3f) {
cout << -1 << endl;
} else {
cout << dis[n].num << " " << dis[n].t << endl;
}
return 0;
}
Explanation:
-
Input Reading:
- The program reads the number of edges
m
and the number of nodes n
.
- It then reads
m
lines, each containing a sequence of integers representing the nodes connected by the edges.
-
Graph Construction:
- The
add
function is used to add edges to the graph. Each edge is stored in the edge
array, and the adjacency list is maintained using the head
and nxt
arrays.
-
Shortest Path Calculation:
- The program uses a modified BFS to find the shortest path from node
1
to node n
.
- The
dis
array keeps track of the minimum number of edges (num
) and the total weight (t
) to reach each node.
- The BFS updates the
dis
array and pushes nodes into the queue if a shorter path is found.
-
Output:
- If the destination node
n
is unreachable, it prints -1
.
- Otherwise, it prints the minimum number of edges and the total weight of the shortest path.
This code should work correctly for the given problem constraints. Make sure to test it with various inputs to ensure its correctness.