1129. 热浪
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
typedef long long ll;
const int M = 1e6 + 10;
#define maxn 3000
#define maxn1 20000
int n, m, start, ending, v, s, num;
int dis[maxn], head[maxn]; // dis数组表示起点到某点的最短路大小
struct Edge {
int v, w, nxt;
} edge[maxn1];
inline void ct(int u, int v, int w) //链式前向星存图。
{
edge[++num].v = v;
edge[num].w = w;
edge[num].nxt = head[u];
head[u] = num;
}
struct node {
int x, y;
bool operator<(
const node& a) const //堆优化重载运算符,使大根堆变成小根堆。
{
return y > a.y;
}
};
void dijkstra(int k) {
memset(dis, 0x3f, sizeof(dis)); //初始化。
dis[k] = 0;
priority_queue<node> q;
q.push((node){k, 0});
node a;
while (!q.empty()) // dijkstra经典套路操作。
{
a = q.top(); //用这个node类型变量提取队首元素。
int u = a.x, d = a.y;
q.pop();
if (d != dis[u])
continue; //一个小优化。
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v;
if ((dis[v] > dis[u] + edge[i].w)) {
dis[v] = dis[u] + edge[i].w;
q.push((node){v, dis[v]});
}
}
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &start, &ending);
for (int i = 1, u, v, w; i <= m; ++i) {
scanf("%d%d%d", &u, &v, &w);
ct(u, v, w); //无向边存两遍。
ct(v, u, w);
}
dijkstra(start);
cout << dis[ending] << endl;
return 0;
}
题解:没什么好说的,就最短路的板子题目,套个dij的板子就可以了
903. 昂贵的聘礼
//这个是代码1
#include <bits/stdc++.h>
using namespace std;
const int N = 110, INF = 0x3f3f3f3f;
int n, m;
int w[N][N], level[N];
int dist[N];
bool st[N];
int dijkstra(int down, int up) {
memset(dist, 0x3f, sizeof(dist));
memset(st, 0, sizeof(st));
dist[0] = 0;
for (int i = 1; i <= n + 1; ++i) {
int t = -1;
for (int j = 0; j <= n; ++j)
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
st[t] = true;
for (int j = 1; j <= n; ++j)
if (level[j] >= down && level[j] <= up)
dist[j] = min(dist[j], dist[t] + w[t][j]);
}
return dist[1];
}
int main() {
cin >> m >> n;
memset(w, 0x3f, sizeof(w));
for (int i = 1; i <= n; ++i)
w[i][i] = 0;
for (int i = 1; i <= n; ++i) {
int price, cnt;
cin >> price >> level[i] >> cnt;
w[0][i] = min(w[0][i], price);
while (cnt--) {
int id, cost;
cin >> id >> cost;
w[id][i] = min(w[id][i], cost);
}
}
int res = INF;
for (int i = level[1] - m; i <= level[1]; ++i)
res = min(res, dijkstra(i, i + m));
cout << res << endl;
return 0;
}
//这个是代码2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e2 + 10, M = 1e4 + 10;
int n, m, s, num, k;
int dis[N], head[N], d[N], p[N];
struct Edge {
int v, w, nxt;
} edge[M];
inline void ct(int u, int v, int w) //链式前向星存图。
{
edge[++num].v = v;
edge[num].w = w;
edge[num].nxt = head[u];
head[u] = num;
}
struct node {
int x, y;
bool operator<(
const node& a) const //堆优化重载运算符,使大根堆变成小根堆。
{
return y > a.y;
}
};
void dijkstra(int l, int r) {
memset(dis, 0x3f, sizeof(dis)); //初始化。
dis[1] = d[1];
priority_queue<node> q;
q.push((node){1, d[1]});
node a;
while (!q.empty()) // dijkstra经典套路操作。
{
a = q.top(); //用这个node类型变量提取队首元素。
int u = a.x, c = a.y;
q.pop();
if (c != dis[u])
continue; //一个小优化。
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v;
if (p[v] < l || p[v] > r)
continue;
if ((dis[v] > dis[u] + d[v] - d[u] + edge[i].w)) {
dis[v] = dis[u] + d[v] - d[u] + edge[i].w;
q.push((node){v, dis[v]});
}
}
}
}
int main() {
scanf("%d%d", &k, &n);
for (int i = 1, u, v, w; i <= n; ++i) {
scanf("%d%d%d", &d[i], &p[i], &m);
for (int j = 0; j < m; ++j) {
scanf("%d%d", &v, &w);
ct(i, v, w);
}
}
int minn = 0x7fffffff;
for (int i = p[1] - k; i <= p[1]; ++i) {
dijkstra(i, i + k);
for (int i = 1; i <= n; ++i) {
minn = min(minn, dis[i]);
}
}
cout << minn << endl;
return 0;
}
题解:是最短路的一点点变形,加上了每个节点的权值,代码1是y总的,代码2是我写的,就是在原有最短路基础上加上对每个节点权值的比较,最后枚举等级区间就可以了。y总的写法是构造出一个虚拟的起点0,从起点0向每一个点建立一条路径,路径权值就为该节点的权值,求从0点到1点的最短路径即可,同样要枚举等级区间。
340. 通信线路
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010, M = 20010;
int n, m, k;
int h[N], e[M], w[M], ne[M], idx;
deque<int> q;
int dist[N];
bool st[N];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool check(int bound) {
memset(st, 0, sizeof(st));
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
q.push_back(1);
while (q.size()) {
int t = q.front();
q.pop_front();
if (st[t])
continue;
st[t] = true;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i], v = w[i] > bound;
if (dist[j] > dist[t] + v) {
dist[j] = dist[t] + v;
if (!v)
q.push_front(j);
else
q.push_back(j);
}
}
}
return dist[n] <= k;
}
int main() {
cin >> n >> m >> k;
memset(h, -1, sizeof(h));
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
}
int l = 0, r = 1e6 + 1;
while (l < r) {
int mid = l + r >> 1;
if (check(mid))
r = mid;
else
l = mid + 1;
}
if (r == 1e6 + 1)
r = -1;
cout << r << endl;
return 0;
}
题解:二分+双端队列广搜,二分答案,然后对每个答案进行搜索,看看是否能在把k条电缆花费变成0的情况下得出答案。这个二分枚举的区间之所以要从0开始,是因为有可能全部电缆花费都被变成了0,于是就不需要花费便能连通,之所以右端点是1e6+1,是因为二分结束的时候,如果r = 1e6+1,便能轻易得出不能连通的结果,但如果右端点只使用1e6,如果r = 1e6,便不能轻易区分出究竟是不能连通导致结果是1e6还是因为可以连通并且结果就是1e6
342. 道路与航线
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 25010, M = 150010, INF = 0x3f3f3f3f;
int n, mr, mp, S;
int v[M], w[M], ne[M], h[M], cnt;
int id[N], bcnt;
vector<int> block[N];
int din[N];
int dist[N];
queue<int> q;
bool st[N];
void add(int a, int b, int c) {
v[++cnt] = b, w[cnt] = c, ne[cnt] = h[a], h[a] = cnt;
}
void dfs(int u, int bid) {
id[u] = bid;
block[bid].push_back(u);
for (int i = h[u]; i; i = ne[i]) {
int j = v[i];
if (!id[j])
dfs(j, bid);
}
}
void dijkstra(int u) {
priority_queue<PII, vector<PII>, greater<PII> > heap;
for (auto ver : block[u]) {
heap.push({dist[ver], ver});
}
while (heap.size()) {
auto k = heap.top();
heap.pop();
int ver = k.y;
if (st[ver])
continue;
st[ver] = true;
for (int i = h[ver]; i; i = ne[i]) {
int j = v[i];
if (dist[j] > dist[ver] + w[i]) {
dist[j] = dist[ver] + w[i];
if (id[j] == id[ver])
heap.push({dist[j], j});
}
if (id[j] != id[ver] && --din[id[j]] == 0)
q.push(id[j]);
}
}
}
void topsort() {
memset(dist, 0x3f, sizeof(dist));
dist[S] = 0;
for (int i = 1; i <= bcnt; ++i) {
if (!din[i])
q.push(i);
}
while (q.size()) {
int t = q.front();
q.pop();
dijkstra(t);
}
}
int main() {
scanf("%d%d%d%d", &n, &mr, &mp, &S);
while (mr--) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
for (int i = 1; i <= n; ++i) {
if (!id[i])
dfs(i, ++bcnt);
}
while (mp--) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
din[id[b]]++;
}
topsort();
for (int i = 1; i <= n; ++i) {
if (dist[i] > INF / 2)
puts("NO PATH");
else
printf("%d\n", dist[i]);
}
return 0;
}
题解:这题是拓扑排序+dijkstra算法,因为题目保证了航线的就不会有负数环,所以非常符合拓扑排序的做法,因为题目保证了不会在航线之间有负数环,所以整个点图可以划分为几个大区域,把每个区域看做是一个点进行拓扑排序,在区域里面进行dijkstra算法求最短路,如此便可以求到其他点的最短路径。