本着练习的角度,使用dijkstra spfa floyd 算法完成本题,巩固一下模板…大佬勿喷....
spfa和dijikstra
只需要将main的sfpa换成dijkstra即可切换
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 210 * 2;
int e[N],ne[N],w[N],h[N],dist[N],idx;
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 n,m;
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;
//cout << ver << endl;
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});
}
}
}
int res = -0x3f3f3f3f;
for(int i = 1; i <= n; i ++)
{
if(dist[i]==0x3f3f3f3f) return -1;
res = max(res,dist[i]);
}
return res;
}
int spfa()
{
queue<int> q;
memset(dist,0x3f,sizeof dist);
dist[1] = 0;
q.push(1);
while(q.size())
{
int 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 = -0x3f3f3f3f;
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 = 1;i <= m; i ++)
{
int a,b,c;
cin >> a >> b >> c;
add(a,b,c);
add(b,a,c);
}
int step = spfa();
cout << step << endl;
return 0;
}
朴素版dijkstra
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int g[N][N];
int dist[N];
int n,m;
bool st[N];
int dijkstra()
{
memset(dist,0x3f,sizeof dist);
dist[1] = 0;
for(int i = 1; i < n; i ++)
{
int t = 0;
for(int j = 1; j <= n; j ++) //找出未被标记中最小的点
{
if(!st[j] && (t == 0 || dist[t] > dist[j]))
t = j;
}
st[t] = true; //标记已使用
//cout << t << " "; //printf调试法
for(int k = 1; k <= n; k ++) //用最小的点,更新其他点
dist[k] = min(dist[k],dist[t] + g[t][k]);
}
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,0x3f3f3f3f,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;
}
Floyd
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110, INF = 0x3f3f3f3f;
int n, m;
int d[N][N];
int main()
{
cin >> n >> m;
memset(d, 0x3f, sizeof d);
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);
}
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 res = 0;
for (int i = 1; i <= n; i ++ )
if (d[1][i] == INF)
{
res = -1;
break;
}
else res = max(res, d[1][i]);
cout << res << endl;
return 0;
}
Floyd算法d[i][i]没初始化为0,会wa掉
dist[1]=0
没有初始化啊可能加强数据了qaq
建议修改一下QAQ
好的,谢谢~
24行少个;号
话说这道题本身的意思好像是每个哨所可能和其他哨所有通信联系。。。理论上来讲应该是不可能出现重边的。所以朴素dijkstra哪里加边好像不需要min。但保险起见,加上也没坏处。
好像……main写成mian了吧
太仔细了吧!
hhh
巨巨加油!
嗯!一起加油!我虽然也巨,但我现在还是巨弱,加油!
这个可以考虑最小生成树吗
模型不一样吧,本题目是求起点到最远的点距离,最小生成树是求把所有点都连接的最小边权和
但是这题也要经过所有点啊
spfa 一开始 st[1] = true 漏了吧?
这个可以不要
嗯,是的
如果不初始化会wa一个点的
很棒啊!
谢谢~
纠正一下,堆优化的dijkstra,最开始 dist[1] = 0 没有初始化,但是竟然过了…
你不是heap 里面初始化了吗…