2021-11-28
Ⅰ . 单源最短路建图方式
1129. 热浪
题目链接:1129. 热浪 CODE
#include<bits/stdc++.h>
using namespace std;
const int N = 3010;
int g[N][N];
int dist[N];
bool st[N];
int n, m, sta, ed;
int dijkstra(int sta,int ed)
{
dist[sta]=0;
for (int i = 0; i < n - 1; i++)
{
int t = -1;
for (int j = 1; j <= n; j++)if (!st[j] && (t == -1 || dist[t] > dist[j]))t = j;
st[t] = true;
for (int j = 1; j <= n; j++)dist[j] = min(dist[j], dist[t] + g[t][j]);
}
return dist[ed];
}
int main()
{
cin >> n >> m >> sta >> ed;
memset(g, 0x3f, sizeof g);
memset(dist,0x3f,sizeof dist);
for (int i = 0; i < m; i++)
{
int a, b, c;
cin>> a >> b >> c;
g[a][b] = g[b][a] = min(g[a][b], c);
}
cout << dijkstra(sta, ed) << endl;
return 0;
}
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 3010;
const int M = 20010;
typedef pair<int, int>PII;
int w[M], e[M], ne[M], h[N], idx;
priority_queue<PII, vector<PII>, greater<PII>>heap;
bool st[N];
int dist[M];
int n, m, sta, ed;
void add(int a, int b, int c)
{
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
int dijkstra(int sta, int ed)
{
heap.push({ 0,sta });
dist[sta] = 0;
while (heap.size())
{
auto t = heap.top();
heap.pop();
int distance = t.x, ver = t.y;
if (st[ver])continue;
st[ver] = true;
for (int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > distance + w[i])
{
dist[j] = distance + w[i];
heap.push({ dist[j],j });
}
}
}
return dist[ed];
}
int main()
{
cin >> n >> m >> sta >> ed;
memset(h, -1, sizeof h);
memset(dist, 0x3f, sizeof dist);
for (int i = 0; i < m; i++)
{
int a, b, c;
cin>> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
cout << dijkstra(sta, ed) << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 3010, M = 20010;
int w[M], e[M], ne[M], h[N], idx;
int dist[N];
bool st[N];
int n, m, sta, ed;
void add(int a, int b, int c)
{
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
int spfa(int sta,int ed)
{
queue<int>q;
q.push(sta);
dist[sta] = 0;
st[sta] = true;
while (q.size())
{
auto t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if (!st[j])
{
st[j] = true;
q.push(j);
}
}
}
}
return dist[ed];
}
int main()
{
cin >> n >> m >> sta >> ed;
memset(h, -1, sizeof h);
memset(dist, 0x3f, sizeof dist);
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
cout << spfa(sta, ed) << endl;
return 0;
}
1128. 信使
题目链接:信使
#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 500;
int e[N], ne[N], w[N], h[N], idx;
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++;
}
int spfa()
{
queue<int>q;
st[1] = true;
dist[1] = 0;
q.push(1);
while (q.size())
{
auto t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if (!st[j])
{
st[j] = true;
q.push(j);
}
}
}
}
int res = 0;
for (int i = 1; i <= n; i++)
{
if (dist[i] == 0x3f3f3f3f)return -1;
res = max(res, dist[i]);
}
return res;
}
int main()
{
memset(h,-1,sizeof h);
memset(dist,0x3f,sizeof dist);
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
cout << spfa() << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int g[N][N];
int n, m;
int dist[N];
bool st[N];
int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for (int i = 0; i < n; i++)
{
int t = -1;
for (int j = 1; j <= n; j++)
{
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
}
st[t] = true;
for (int j = 1; j <= n; j++)
dist[j] = min(dist[j], dist[t] + g[t][j]);
}
int res = -1;
for (int i = 1; i <= n; i++)
{
if (dist[i] == 0x3f3f3f3f)return -1;
res = max(res, dist[i]);
}
return res;
}
int main()
{
cin >> n >> m;
memset(g, 0x3f, sizeof g);
for (int i = 1; i <= m; i ++)
{
int a, b, c;
cin >> a >> b >> c;
if (a == b)continue;
g[a][b] = min(g[a][b], c);
g[b][a] = min(g[b][a], c);
}
cout << dijkstra() << endl;
return 0;
}
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 210 * 2;
typedef pair<int, int>PII;
int e[N], h[N], ne[N], w[N], dist[N], idx;
bool st[N];
int n, m;
void add(int a, int b, int c)
{
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx++;
}
int dijkstra()
{
priority_queue<PII, vector<PII>, greater<PII>>heap;
memset(dist, 0x3f, sizeof dist);
heap.push({ 0,1 });
dist[1] = 0;
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.y, distance = t.x;
if (st[ver])continue;
for (int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > distance + w[i])
{
dist[j] = distance + w[i];
heap.push({ dist[j],j });
}
}
}
int res = -1;
for (int i = 1; i <= n; i++)
{
if (dist[i] == 0x3f3f3f3f)return -1;
res = max(res, dist[i]);
}
return res;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
cout << dijkstra() << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m;
int d[N][N];
void floyd()
{
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
int main()
{
cin >> n >> m;
memset(d, 0x3f, sizeof d);
for (int i = 1; i <= n; i++)d[i][i] = 0;
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
d[a][b] = d[b][a] = min(d[a][b], c);
}
floyd();
int res = -1;
for(int i=1;i<=n;i++)
{
if (d[1][i] == 0x3f3f3f3f)
{
cout << -1 << endl;
return 0;
}
res = max(res, d[1][i]);
}
cout << res << endl;
return 0;
}
|