【C++】《图论习题集》更新至 单源最短路的建图方式

1 篇文章 0 订阅
1 篇文章 0 订阅

2021-11-28

Ⅰ . 单源最短路建图方式

1129. 热浪

题目链接:1129. 热浪
CODE

//朴素版dijkstra
#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;
}
//堆优化dijkstra
#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;
}
//spfa
#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. 信使

题目链接:信使

//spfa
#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;
}
//朴素dijkstra
#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;
}
//堆优化dijkstra
#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;
}
//floyd
#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;
}

1127. 香甜的黄油

题目链接:香甜的黄油

//spfa
#include <bits/stdc++.h>
using namespace std;
const int N = 810, M = 3000;
int h[N], ne[M], e[M], idx, w[M];
int dist[N];
bool st[N];
int id[N], T, n, m;
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)
{
	memset(dist, 0x3f, sizeof dist);
	memset(st, 0, sizeof st);
	queue<int>q;
	q.push(sta);
	st[sta] = true;
	dist[sta] = 0;
	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])continue;
				st[j] = true;
				q.push(j);
			}
		}
	}
	int res = 0;
	for (int i = 1; i <= T; i++)
	{
		if (dist[id[i]] == 0x3f3f3f3f)return 0x3f3f3f3f;
		res += dist[id[i]];
	}
	return res;
}
int main()
{
	memset(h, -1, sizeof h);
	cin >> T >> n >> m;
	for (int i = 1; i <= T; i++)cin >> id[i];
	for (int i = 1; i <= m; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
		add(b, a, c);
	}
	int res = 0x3f3f3f3f;
	for (int i = 1; i <= n; i++)
		res = min(res, spfa(i));
	cout << res << endl;
	return 0;
}
//dijkstra
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 1000, M = 3000, INF = 0x3f3f3f3f;
typedef pair<int, int>PII;
int dist[N];
bool st[N];
int h[N], ne[M], w[M], e[M], idx;
int T, n, m, id[N];
void add(int a, int b, int c)
{
	e[idx] = b;
	ne[idx] = h[a];
	w[idx] = c;
	h[a] = idx++;
}
int dijkstra(int x)
{
	memset(dist, 0x3f, sizeof dist);
	memset(st, 0, sizeof st);
	dist[x] = 0;
	priority_queue<PII, vector<PII>, greater<PII>>heap;
	heap.push({ dist[x],x });
	while (heap.size())
	{
		auto t = heap.top();
		heap.pop();
		int ver = t.y, distance = t.x;
		if (st[ver])continue;
		st[ver] = true;
		for (int i = h[ver]; ~i; i = ne[i])
		{
			int j = e[i];
			if (dist[j] > dist[ver] + w[i])
			{
				dist[j] = dist[ver] + w[i];
				heap.push({ dist[j],j });
			}
		}
	}
	int res = 0;
	for (int i = 1; i <= T; i++)
	{
		if (dist[id[i]] == 0x3f3f3f3f)return 0x3f3f3f3f;
		res += dist[id[i]];
	}
	return res;
}
int main()
{
	memset(h, -1, sizeof h);
	cin >> T >> n >> m;
	for (int i = 1; i <= T; i++)cin >> id[i];
	for (int i = 0; i < m; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
		add(b, a, c);
	}
	int res = INF;
	for (int i = 1; i <= n; i++)res = min(res, dijkstra(i));
	cout << res << endl;
	return 0;
}

1126. 最小花费

题目链接:最小花费
CODE

#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
double g[N][N];
bool st[N];
double dist[N];
int n, m;
int a, b, c;
int s, t;
void dijkstra()
{
	dist[s] = 1;
	for (int i = 0; i < n; i++)
	{
		int t = -1;
		for (int j = 1; j <= n; j++)
			if (!st[j] && (t == -1 || dist[j] > dist[t]))
				t = j;
		st[t] = true;
		for (int j = 1; j <= n; j++)
			dist[j] = max(dist[j], dist[t] * g[t][j]);
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		cin >> a >> b >> c;
		double z = (100.0 - c) / 100;
		g[a][b] = g[b][a] = max(g[a][b], z);
	}
	cin >> s >> t;
	dijkstra();
	printf("%.8f", 100 / dist[t]);
	return 0;
}

920 . 最优乘车

题目链接:最有乘车
CODE

#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int n, m;
int stop[N];
bool g[N][N];
int dist[N];
void bfs()
{
	memset(dist, 0x3f, sizeof dist);
	queue<int>q;
	q.push(1);
	dist[1] = 0;
	while (q.size())
	{
		auto t = q.front();
		q.pop();
		for (int i = 1; i <= n; i++)
		{
			if (g[t][i] && dist[i] > dist[t] + 1)
			{
				dist[i] = dist[t] + 1;
				q.push(i);
			}
		}
	}
}
int main()
{
	cin >> m >> n;
	string line;
	getchar();
	while (m--)
	{
		getline(cin, line);
		stringstream ssin(line);
		int cnt = 0, p;
		while (ssin >> p)stop[cnt++] = p;
		for (int i = 0; i < cnt; i++)
		{
			for (int j = i + 1; j < cnt; j++)
			{
				g[stop[i]][stop[j]] = true;
			}
		}
	}
	bfs();
	if (dist[n] == 0x3f3f3f3f)puts("NO");
	else cout << max(dist[n] - 1, 0) << endl;
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值