#define N 105 #define INF 0x3f3f3f3f int g[N][N]; // memset(g, 0x3f, sizeof(g)); int dis[N], vis[N]; voiddijkstra(int n, int s){ memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[s] = 0; for (int i = 0; i < n; i++) { int u = 0, mind = INF; for (int j = 0; j < n; j++) if (!vis[j] && dis[j] < mind) u = j, mind = dis[j]; vis[u] = 1; for (int v = 0; v < n; v++) { if (v == u) continue; if (dis[v] > dis[u] + g[u][v]) dis[v] = dis[u] + g[u][v]; } } }
classSolution { public: vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int src, int dst, int target) { memset(g, 0x3f, sizeof(g)); for (auto& i:edges) { g[i[0]][i[1]] = g[i[1]][i[0]] = i[2]; if (i[2] == -1) g[i[0]][i[1]] = g[i[1]][i[0]] = INF; } dijkstra(n, src); if (dis[dst] < target) return {};
memset(g, 0x3f, sizeof(g)); for (auto& i:edges) { g[i[0]][i[1]] = g[i[1]][i[0]] = i[2]; if (i[2] == -1) g[i[0]][i[1]] = g[i[1]][i[0]] = 1; } dijkstra(n, src); if (dis[dst] > target) return {}; memset(g, 0x3f, sizeof(g)); for (auto& i:edges) { g[i[0]][i[1]] = g[i[1]][i[0]] = i[2]; } vector<int> dis2(n, INF); dis2[src] = 0; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { int u = 0, mind = INF; for (int j = 0; j < n; j++) if (!vis[j] && dis2[j] < mind) u = j, mind = dis2[j]; vis[u] = 1; for (int v = 0; v < n; v++) { if (v == u) continue; if (g[u][v] == -1) g[v][u] = g[u][v] = max(1, target - dis2[u] - dis[dst] + dis[v]); if (dis2[v] > dis2[u] + g[u][v]) dis2[v] = dis2[u] + g[u][v]; } } for (auto& i:edges) { i[2] = g[i[0]][i[1]]; if (i[2] == -1) i[2] = 1; } return edges; } };
#define N 105 #define INF 0x3f3f3f3f3f3f3f3f long g[N][N]; // memset(g, 0x3f, sizeof(g)); long dis[N], vis[N]; voiddijkstra(int n, int s){ memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[s] = 0; for (int i = 0; i < n; i++) { long u = 0, mind = INF; for (int j = 0; j < n; j++) if (!vis[j] && dis[j] < mind) u = j, mind = dis[j]; vis[u] = 1; for (int v = 0; v < n; v++) { if (v == u) continue; if (dis[v] > dis[u] + g[u][v]) dis[v] = dis[u] + g[u][v]; } } }
classSolution { public: vector<vector<int>> modifiedGraphEdges(int n, vector<vector<int>>& edges, int src, int dst, int target) { memset(g, 0x3f, sizeof(g)); for (auto& i:edges) { g[i[0]][i[1]] = g[i[1]][i[0]] = i[2]; if (i[2] == -1) g[i[0]][i[1]] = g[i[1]][i[0]] = INF; } dijkstra(n, src); if (dis[dst] < target) return {};
memset(g, 0x3f, sizeof(g)); for (auto& i:edges) { g[i[0]][i[1]] = g[i[1]][i[0]] = i[2]; if (i[2] == -1) g[i[0]][i[1]] = g[i[1]][i[0]] = 1; } dijkstra(n, src); if (dis[dst] > target) return {};
auto dj = [&](long a, long b) { memset(g, 0x3f, sizeof(g)); for (auto& i:edges) { g[i[0]][i[1]] = g[i[1]][i[0]] = i[2]; } memset(vis, 0, sizeof(vis)); memset(dis, 0x3f, sizeof(dis)); dis[src] = 0; for (int i = 0; i < n; i++) { long u = 0, mind = INF; for (int j = 0; j < n; j++) if (!vis[j] && dis[j] < mind) u = j, mind = dis[j]; vis[u] = 1; for (int v = 0; v < n; v++) { if (v == u) continue; if (g[u][v] == -1) { g[u][v] = g[v][u] = a; if (b) { g[u][v] = g[v][u] = a+1; b--; } } if (dis[v] > dis[u] + g[u][v]) dis[v] = dis[u] + g[u][v]; } } };
int ncnt = 0; for (auto& i:edges) { if (i[2] == -1) ncnt++; } long l = ncnt, r = ncnt*1e10; while (l<r) { long m = l+r>>1; long a = m/ncnt, b = m%ncnt; dj(a, b); // cout << dis[dst] << " " << target << "\n"; if (dis[dst] >= target) r = m; else l = m+1; } if (ncnt) dj(r/ncnt, r%ncnt); // cout << dis[dst] << " " << target << "\n"; for (auto& i:edges) { i[2] = g[i[0]][i[1]]; if (i[2] == -1) i[2] = 1; } return edges; } };