#define _CRT_SECURE_NO_WARNINGS#include<bits/stdc++.h>using namespace std;const int LEN = 2e5 + 5;struct Edge {int begin, end, weight;int next;}edge[LEN];int n, m, head[LEN], dis[LEN];int s, e;bool vis[LEN];int cnt[LEN];queue<int>q;bool spfa(int start) {q.push(start);vis[start] = true;dis[start] = 0;while (!q.empty()) {int vertex = q.front();q.pop();vis[vertex] = false;for (int i = head[vertex]; i; i = edge[i].next) {int end = edge[i].end;int tmp = dis[vertex] + edge[i].weight;if (tmp < dis[end]) {dis[end] = tmp;if (!vis[end]) {q.push(end);vis[end] = true;if (++cnt[end] > n)return false;}}}}return true;}int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);cin >> n >> m >> s >> e;fill(dis + 1, dis + 1 + n, 0x3f3f3f3f);for (int i = 1; i <= m; i++) {int u, v, w;cin >> u >> v >> w;edge[i].begin = u;edge[i].end = v;edge[i].weight = w;edge[i].next = head[u];head[u] = i;}if (!spfa(s))cout << \"FAIL\" << endl;else cout << dis[e] << endl;return 0;}
E.G.Postman Delivers letters
5 102 3 51 5 53 5 61 2 81 3 85 3 44 1 84 5 33 5 65 4 2
83
Code
#define _CRT_SECURE_NO_WARNINGS#include<bits/stdc++.h>using namespace std;int n, m;const int N = 1e3 + 5;const int M = 1e5 + 5;const int INF = 0x3f3f3f3f;struct Edge {int des, dis;Edge() {}Edge(int a, int b) :des(a), dis(b) {}};vector<Edge>v1[M];vector<Edge>v2[M];bool vis[N];int dis1[N], dis2[N];void Spfa(int x, vector<Edge>v[M], int dis[N]) {queue<int>q;q.push(x);vis[x] = true;dis[x] = 0;while (!q.empty()) {int vertex = q.front();q.pop();vis[vertex] = false;for (vector<Edge>::iterator it = v[vertex].begin(); it != v[vertex].end(); ++it) {int end = it->des;int tmp = dis[vertex] + it->dis;if (tmp < dis[end]) {dis[end] = tmp;if (!vis[end]) {q.push(end);vis[end] = true;}}}}}int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);cin >> n >> m;for (int i = 1; i <= m; i++) {int u, v, w;cin >> u >> v >> w;v1[u].push_back(Edge{ v,w });v2[v].push_back(Edge{ u,w });}fill(dis1 + 1, dis1 + 1 + n, INF);fill(dis2 + 1, dis2 + 1 + n, INF);Spfa(1, v1, dis1);memset(vis, 0, sizeof(vis));Spfa(1, v2, dis2);int ans = 0;for (int i = 1; i <= n; i++) {ans += (dis1[i] + dis2[i]);}cout << ans << endl;return 0;}
E.G.The Shortest Path
题目背景
狗哥做烂了最短路,突然机智的考了 Bosh 一道,没想到把 Bosh 考住了…你能帮 Bosh 解决吗?
题目描述
给定 n 个点的带权有向图,求从 1 到 n 的路径中边权之积最小的简单路径。
输入格式
第一行读入两个整数 n,m,表示共 n 个点 m 条边。 接下来 m 行,每行三个正整数 x,y,z,表示点 x 到点 y 有一条边权为z的边。
输出格式
输出仅包括一行,记为所求路径的边权之积,由于答案可能很大,因此狗哥仁慈地让你输出它模 99879987 的余数即可。
废话当然是一个数了w
输入输出样例
3 31 2 32 3 31 3 10
9
说明/提示
Code
#define _CRT_SECURE_NO_WARNINGS#include<bits/stdc++.h>using namespace std;int n, m;const int N = 1e3 + 5;const int M = 1e6 + 5;const int MOD = 9987;const int INF = 0x3f3f3f3f;struct Edge {int end, dis;int next;}edge[M];int head[N];double dis[N];bool vis[N];int pos[N][2];queue<int>q;void Spfa(int x) {dis[x] = 0;q.push(x);vis[x] = true;while (!q.empty()) {int vertex = q.front();q.pop();vis[vertex] = false;for (int i = head[vertex]; i; i = edge[i].next) {int end = edge[i].end;double tmp = dis[vertex] + log(edge[i].dis);if (tmp < dis[end]) {dis[end] = tmp;pos[end][0] = vertex;pos[end][1] = edge[i].dis;if (!vis[end]) {q.push(end);vis[end] = true;}}}}}int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);cin >> n >> m;fill(dis + 1, dis + n + 1, INF);for (int i = 1; i <= m; i++) {int u, v, w;cin >> u >> v >> w;edge[i].end = v;edge[i].dis = w;edge[i].next = head[u];head[u] = i;}Spfa(1);int ans = 1, index = n;while (index != 1) {ans *= pos[index][1];ans %= MOD;index = pos[index][0];}cout << ans%MOD << endl;return 0;}