// P3371#include <iostream>#include <vector>#include <queue>#include <climits>using namespace std;
#define MAXN 10002#define pii pair<int,int>
vector<pii> adj[MAXN];
long long dst[MAXN];
bool visited[MAXN];
int main() {
int n, m, s; cin >> n >> m >> s;
while(m--) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v,w});
}
for (int i = 1; i <= n; i++) {
dst[i] = INT_MAX;
}
dst[s] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0,s});
while (!pq.empty()) {
int u = pq.top().second; pq.pop();
if (visited[u]) continue;
visited[u] = true;
for (auto vw : adj[u]) {
int v = vw.first, w = vw.second;
if (dst[u]+w < dst[v]) {
dst[v] = dst[u] + w;
pq.push({dst[v],v});
}
}
}
for (int i = 1; i <= n; i++) {
cout << dst[i] << ' ';
}
return 0;
}
C++
// P1608#include <iostream>#include <vector>#include <queue>using namespace std;
#define MAXN 2002#define pii pair<int,int>
vector<pii> adj[MAXN];
int dst[MAXN], cnt[MAXN];
bool visited[MAXN];
int main() {
int n, m; cin >> n >> m;
while (m--) {
int u, v, w; cin >> u >> v >> w;
bool found = false;
for (pii &vw : adj[u]) {
if (v == vw.first) {
found = true;
vw.second = min(vw.second,w);
break;
}
}
if (!found)
adj[u].push_back({v,w});
}
// dijkstrafor (int i = 1; i <= n; i++) {
dst[i] = 1e9;
}
dst[1] = 0, cnt[1] = 1;
priority_queue<pii,vector<pii>,greater<pii>> pq;
pq.push({0,1});
while (!pq.empty()) {
int u = pq.top().second; pq.pop();
if (visited[u]) continue;
visited[u] = true;
for (pii vw : adj[u]) {
int v = vw.first, w = vw.second;
if (dst[u]+w < dst[v]) {
dst[v] = dst[u]+w, cnt[v] = cnt[u];
pq.push({dst[v],v});
} else if (dst[u]+w == dst[v]) {
cnt[v] += cnt[u];
}
}
}
if (dst[n] == 1e9) {
cout << "No answer" << endl;
} else {
cout << dst[n] << ' ' << cnt[n] << endl;
}
return 0;
}
C++
// P1629#include <iostream>#include <vector>#include <queue>using namespace std;
#define MAXN 1002#define pii pair<int,int>
vector<pii> adj1[MAXN], adj2[MAXN];
int dst1[MAXN], dst2[MAXN];
bool visited1[MAXN], visited2[MAXN];
void dijkstra(vector<pii> adj[], int dst[], bool visited[], int n) {
for (int i = 1; i <= n; i++) {
dst[i] = 1e9;
}
dst[1] = 0;
priority_queue<pii,vector<pii>,greater<pii>> pq;
pq.push({0,1});
while (!pq.empty()) {
int u = pq.top().second; pq.pop();
if (visited[u]) continue;
visited[u] = true;
for (auto vw : adj[u]) {
int v = vw.first, w = vw.second;
if (dst[u]+w < dst[v]) {
dst[v] = dst[u] + w;
pq.push({dst[v],v});
}
}
}
}
int main() {
int n, m; cin >> n >> m;
while(m--) {
int u, v, w; cin >> u >> v >> w;
adj1[u].push_back({v,w});
adj2[v].push_back({u,w});
}
dijkstra(adj1,dst1,visited1,n);
dijkstra(adj2,dst2,visited2,n);
int ans = 0;
for (int i = 1; i <= n; i++) {
ans += dst1[i] + dst2[i];
}
cout << ans << endl;
return 0;
}
C++
// P4568#include <iostream>#include <vector>#include <queue>#include <tuple>using namespace std;
#define MAXN 10002#define MAXK 11#define pii pair<int,int>#define tiii tuple<int,int,int>
vector<pii> adj[MAXN];
int dst[MAXN][MAXK];
bool visited[MAXN][MAXK];
void dijkstra(int s, int n, int k) {
for (int i = 0; i < MAXN; i++) {
for (int j = 0; j < MAXK; j++) {
dst[i][j] = 1e9;
}
}
priority_queue<tiii, vector<tiii>, greater<tiii>> pq;
for (int i = 0; i <= k; i++) {
dst[s][i] = 0;
pq.push({0,s,i});
}
while (!pq.empty()) {
int d, u, curr_k;
tie(d, u, curr_k) = pq.top(); pq.pop();
if (visited[u][curr_k]) continue;
visited[u][curr_k] = true;
for (pii vw : adj[u]) {
int v = vw.first, w = vw.second;
// 情况1:不使用免费,留在当前层if (dst[u][curr_k]+w < dst[v][curr_k]) {
dst[v][curr_k] = dst[u][curr_k] + w;
pq.push({dst[v][curr_k],v,curr_k});
}
// 情况2:使用免费(如果还有剩余次数)if (curr_k < k && dst[u][curr_k] < dst[v][curr_k+1]) {
dst[v][curr_k+1] = dst[u][curr_k];
pq.push({dst[u][curr_k],v,curr_k+1});
}
}
}
}
int main() {
int n, m, k, s, t;
cin >> n >> m >> k >> s >> t;
while(m--) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
dijkstra(s,n,k);
int ans = 1e9;
for (int i = 0; i <= k; i++) {
ans = min(ans,dst[t][k]);
}
cout << ans << endl;
return 0;
}
C++
// P2296#include <iostream>#include <vector>#include <queue>using namespace std;
#define MAXN 200002#define pii pair<int,int>
vector<int> adj1[MAXN], adj2[MAXN]; // 原图和反向图的邻接表bool valid[MAXN], allow[MAXN]; // 是否能到达终点t, 可否出现在最短路中int dst[MAXN]; // 距离数组bool visited[MAXN]; // 是否访问过int main() {
int n, m; cin >> n >> m;
while (m--) {
int u, v; cin >> u >> v;
adj1[u].push_back(v);
adj2[v].push_back(u);
}
int s, t; cin >> s >> t;
// 步骤1: 从 t 开始 BFS 反向图,找出所有能到达 t 的节点
queue<int> q; q.push(t); valid[t] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v : adj2[u]) {
if (!valid[v]) {
valid[v] = true; q.push(v);
}
}
}
// 步骤2: 筛选出允许的节点(所有出边终点均合法)for (int u = 1; u <= n; u++) {
if (!valid[u]) continue;
bool flag = true;
for (int v : adj1[u]) {
if (!valid[v]) {
flag = false;
break;
}
}
allow[u] = flag;
}
// 起点或终点不合法直接返回if (!allow[s] || !allow[t]) {
cout << -1 << endl;
return 0;
}
// 步骤3: Dijkstra求最短路(边权为1)for (int i = 1; i <= n; i++) {
dst[i] = 1e9;
}
dst[s] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0,s});
while (!pq.empty()) {
int u = pq.top().second; pq.pop();
// 提前终止条件:到达终点if (u == t) {
cout << dst[u] << endl;
return 0;
}
if (visited[u]) continue;
visited[u] = true;
for (int v : adj1[u]) {
if (allow[v] && dst[u] + 1 < dst[v]) {
dst[v] = dst[u] + 1;
pq.push({dst[v], v});
}
}
}
// 无法到达终点
cout << -1 << endl;
return 0;
}
C++
// P2865#include <iostream>#include <vector>#include <queue>#include <tuple>using namespace std;
#define MAXN 5002#define pii pair<int,int>
vector<pair<int,int>> adj[MAXN];
int dst[2][MAXN]; // dist[0][i]: 最短,dist[1][i]: 次短void dijkstra() {
for (int i = 1; i <= MAXN; i++) {
dst[0][i] = dst[1][i] = 1e9;
}
dst[0][1] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0,1});
while (!pq.empty()) {
int dist, u; tie(dist,u) = pq.top(); pq.pop();
if (dist > dst[1][u]) continue; // 当前状态无效for (auto vw : adj[u]) {
int v, w; tie(v,w) = vw;
int new_d = dist + w;
if (new_d < dst[0][v]) { // 更新最短
dst[1][v] = dst[0][v];
dst[0][v] = new_d;
pq.push({dst[0][v], v});
pq.push({dst[1][v], v});
} else if (new_d > dst[0][v] && new_d < dst[1][v]) { // 更新次短
dst[1][v] = new_d;
pq.push({dst[1][v], v});
}
}
}
}
int main() {
int n, m; cin >> n >> m;
while (m--) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
dijkstra();
cout << dst[1][n] << endl;
return 0;
}
C++
// P1462#include <iostream>#include <vector>#include <queue>#include <algorithm>using namespace std;
#define ll long long#define pli pair<ll,int>#define MAXN 10002int n, m, blood; // 城市数,道路数,初始血量int cost[MAXN]; // 各城市费用
vector<pli> adj[MAXN]; // 邻接表存图// 带费用限制的Dijkstra(返回是否能在max_cost限制下到达终点)bool check(int max_cost) {
priority_queue<pli, vector<pli>, greater<pli>> pq;
vector<ll> dst(n+1, 4e18);
dst[1] = 0; pq.push({0,1});
while(!pq.empty()) {
int curr_dst = pq.top().first, u = pq.top().second; pq.pop();
if(u == n) break; // 提前终止if(curr_dst > dst[u]) continue; // 无效状态for(auto vw : adj[u]) { // 遍历所有邻接道路int v = vw.first; ll w = vw.second;
if(cost[v] > max_cost) continue; // 跳过费用超限城市if(dst[u] + w < dst[v]) { // 松弛操作
dst[v] = dst[u] + w;
pq.push({dst[v], v});
}
}
}
return dst[n] <= blood; // 总消耗是否在血量范围内
}
int main() {
cin >> n >> m >> blood;
for(int i = 1; i <= n; i++) cin >> cost[i];
while (m--) {
int u, v; ll w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
// 二分答案框架int left = max(cost[1], cost[n]); // 必须包含起点和终点int right = *max_element(cost+1, cost+n+1);
int ans = -1;
while(left <= right) {
int mid = (left + right) / 2;
if (check(mid)) { // 如果当前费用可行
ans = mid;
right = mid - 1; // 尝试更小费用
} else {
left = mid + 1; // 否则,尝试更大的费用
}
}
if (ans == -1)
cout << "AFK" << endl;
else
cout << ans << endl;
return 0;
}
C++
// P1948#include <iostream>#include <vector>#include <queue>using namespace std;
#define pii pair<int,int>#define MAXN 1002int n, m, k;
vector<pii> adj[MAXN];
int dst[MAXN];
bool dijkstra(int max_len) {
for (int i = 1; i <= n; i++) {
dst[i] = 1e9;
}
dst[1] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0,1});
while (!pq.empty()) {
int cnt = pq.top().first, u = pq.top().second; pq.pop();
if (u == n) return cnt <= k; // 找到终点,返回是否满足条件if (cnt > dst[u]) continue; // 无效状态for (auto vw : adj[u]) {
int v = vw.first, w = vw.second;
int new_cnt = cnt + ((w > max_len) ? 1 : 0);
if (new_cnt < dst[v]) {
dst[v] = new_cnt;
pq.push({new_cnt,v});
}
}
}
return false; // 无法到达终点
}
int main() {
cin >> n >> m >> k;
int max_edge = 0;
while (m--) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
max_edge = max(max_edge,w);
}
int left = 0, right = max_edge;
int ans = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (dijkstra(mid)) {
ans = mid;
right = mid - 1; // 尝试更小的阈值
} else {
left = mid + 1; // 需要增大阈值
}
}
cout << (ans != -1 ? ans : -1) << endl;
return 0;
}