鍥捐 鏈€鐭矾 鍩虹
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍥捐 鏈€鐭矾 鍩虹相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/fat' title='fat'>fat
鏃堕棿 turn 鐢宠 鍙屽悜 -o spfa鍥捐鍩虹 锛?鏈€鐭矾
(v_i) 鍜屾渶鍚庝竴涓《鐐?(v_j) 鏄悓涓€涓《鐐癸紝鍒欑О杩欐潯璺緞涓哄洖璺?/p>
杩為€?/strong> 锛?涓や釜鐐硅繛閫氾紝鎸?u 鍜?v 鐩镐簰鍙揪
鍥捐繛閫?/strong> 锛氭棤鍚戝浘 浠绘剰涓や釜鑺傜偣 鐩镐簰鍙揪 锛?/p>
寮鸿繛閫?/strong> 锛?鏈夊悜鍥?浠绘剰 涓や釜鑺傜偣 鐩镐簰鍙揪 閭绘帴鐭╅樀 锛堜簩缁存暟缁勶級 瀵逛簬 n 涓《鐐癸紝闇€瑕?(O(n^2)) 绌洪棿 涓ラ噸娴垂绌洪棿锛岄€傜敤浜?绋犲瘑鍥?/strong> 锛?鎴栬€呮槸 锛岄鐩殑杈撳叆 宸茬煩闃电殑鏂瑰紡缁欏嚭 G[N] [N] 閭绘帴琛?/strong> 瀵瑰浘涓殑姣忎釜椤剁偣閮藉缓绔嬩竴涓崟閾捐〃锛屽瓨鍌ㄨ繖涓《鐐圭殑杩炲嚭鐨勭偣 vector vector 鑷韩搴曞眰鍑芥暟鐨勭己闄凤紝 姣忔鎵╁ぇ绌洪棿锛岄兘浼歝opy涓€杈癸紝鐒跺悗鎵╁ぇ鍘熸潵绌洪棿鐨勪袱鍊?/p> vector 閾惧紡鍓嶅悜鏄?/strong> 锛堟暟缁勬ā鎷熼偦鎺ヨ〃锛?/p>
鍓嶇疆鐭ヨ瘑锛屾暟缁勬ā鎷熼摼琛?澶存彃娉? DFS 锛堟眰杩為€氬潡锛夛紝姹傛鎷夊洖璺?涓?鍝堝瘑椤垮洖璺?/p>
渚嬮 锛?https://vjudge.net/problem/UVA-572 BFS 锛堣竟鏉冧负 1 鐨勬渶鐭矾绠楁硶 锛岄槦鍒楀疄鐜?锛?/p>
渚嬮 锛?https://vjudge.net/problem/OpenJ_Bailian-3752 鍫嗕紭鍖杁ij Bell-Ford O(ne) 鍙互澶勭悊璐熸潈杈癸紝涓嶈兘澶勭悊璐熸潈鍥炶矾锛岃礋鏉冨洖璺病鏈夋渶鐭矾 // 鍙兘瀛樿竟 绠楁硶姝ラ锛?/p>
杩涜n-1娆℃澗寮涙搷浣?/p>
姣忔寰幆 m 娆?锛屾洿鏂版瘡鏉¤竟鐨勮窛绂?/p>
SPFA 锛堥槦鍒椾紭鍖栫増鏈殑 Bell-Ford锛?鍙互鍒ゆ柇璐熺幆锛屽鏄撹缃戞牸鍥惧崱锛?/p>
spfa https://blog.csdn.net/u011644423/article/details/38345631 ? https://skywt.cn/posts/spfasummary/ POJ 1511锛?259 澶氭簮姹囨渶鐭矾 DP鎬濇兂锛屾毚鍔?,鍙兘鐢?閭绘帴鐭╅樀瀛樺浘 Floyed O(n^3), 杩樺彲浠ョ敤鏉ュ垽鏂紝鍥句腑鐨勪袱涓偣鏄惁鐩歌繛 涔犻鎺ㄨ崘锛?/p>
HDU 1869锛?665锛?596锛?734 POJ 1125 鏈€鐭矾鐨勫簲鐢?锛?宸垎绾︽潫绯荤粺锛堜笉绛夊紡姹傝В锛?/p>
kuangbin鏈€鐭矾涓撻 锛宧ttps://vjudge.net/contest/324762 鎺ㄨ崘闃呰锛?/p>
https://www.luogu.org/blog/wym483739/xue-tu-lun-ni-zhen-di-liao-xie-zui-duan-lu-ma-post https://studyingfather.blog.luogu.org/some-coding-tips-for-oiers some tips for oiers https://www.luogu.org/blog/chengni5673/tu-lun-di-xiao-ji-qiao-yi-ji-kuo-zhan 鍥捐灏忔妧宸?/p>
https://www.luogu.org/blog/little-sun/dijkstra dij璇﹁В https://www.luogu.org/blog/encore/io-you-hua-nei-suo-shi oi 浼樺寲 https://oi-wiki.org/graph oi-wiki-鍥捐涓撻鍥剧殑瀛樺偍缁撴瀯锛?/h3>
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e3 + 233,INF = 0x3f3f3f3f; //INF 鏈€澶у€?int g[N][N];
int main(){
int n,m,x,y,w;
memset(g,0x3f,sizeof g); // 鍒濆鍖栧浘锛屽叏閮戒笉鑱旈€? cin >> n >> m;
while(m--){
cin >> x >> y >> w;
g[x][y] = w; // 鍗曞悜杈癸紝x to y 锛屾潈閲?涓?w
g[y][x] = w; // 涓ゆ潯閮藉啓锛岀瓑浜庡弻鍚戣竟锛?鍥句腑閮戒负鍙屽悜杈癸紝灏辨垚鏃犲悜鍥句簡銆? }
for(int i = 1;i <= n; ++i){
for(int j = 1;j <= n; ++j){
cout << g[i][j] <<" ";
}
cout << endl;
}
return 0;
}
缂虹偣
1.STL浼氱暐鎱竴浜?br />
2.娴垂绌洪棿锛岀敱浜巚ector鐢宠绌洪棿鐨勬柟寮忔槸涓ゅ€嶆墿瀹癸紝閬囧埌鍗$┖闂寸殑棰樼洰鐨勬椂鍊欎細璺?/p>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1e3 + 233,INF = 0x3f3f3f3f;
struct edge
{
int node ,weight;
edge(int node_,int weight_)://鏋勯€犲嚱鏁帮紝璁╂垜浠彲浠ョ洿鎺ョ粰缁撴瀯浣撹祴鍊? node(node_),weight(weight_){}
};
vector<edge> v[N];
int n,m;
int main()
{
cin >> n >> m;
while(m--)
{
int x,y,w;
cin >> x >> y >> w;
v[x].push_back(edge(y,w));//妯℃嫙閾捐〃锛屽瓨杈? //v[y].push_back(edge(x,w));//鍙屽悜
}
for(int i = 1;i <= n; ++i){
for(int j = 0;j < v[i].size(); ++j){
cout << i << " " << v[i][j].node <<" " << v[i][j].weight <<" ";
}
cout << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1e3 + 233,INF = 0x3f3f3f3f;
int e[N],ne[N],h,idx;
// head 琛ㄧず澶寸粨鐐圭殑涓嬫爣
// e[i] 琛ㄧず鑺傜偣i鐨勫€?// ne[i] 琛ㄧず鑺傜偣i鐨刵ext鎸囬拡鏄灏?// idx 瀛樺偍褰撳墠宸茬粡鐢ㄥ埌浜嗗摢涓偣
void init(){ // 鍒濆鍖栦负澶磋妭鐐?涓?-1
h = -1,idx = 0;
}
void add(int x){
e[idx] = x;// 淇濆瓨x鐨勫€?鍒?idx 杩欎釜鎸囬拡鐨勪綅缃? ne[idx] = h;// idx 鐨勪笅涓€涓寚閽?涓?澶磋妭鐐? h = idx++; // 澶磋妭鐐?= idx 鎸囬拡 锛?idx ++
}
int main(){
int n ,x;
cin >> n;
init();
while(n--){
cin >> x;
add(x);
}
for(int i = h;i != -1; i = ne[i]){
cout << e[i] << " ";
}
return 0;
}
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1e3 + 233,INF = 0x3f3f3f3f;
int e[N],ne[N],h[N],w[N],idx;//鏁扮粍妯℃嫙閭绘帴琛紝 h浠h〃 n涓ご鑺傜偣
// e瀛樻瘡鏉¤竟锛宯e鎸囧悜涓嬩竴鏉¤竟锛宧鏄摼琛ㄥご锛寃鏄瘡涓《鐐圭殑鏉冮噸锛宨dx鏄储寮曞€?void add(int a,int b,int c){// 閲囩敤澶存彃娉? e[idx] = b;
ne[idx] = h[a];
w[b] = c;
h[a] = idx++;
}
int n,m;
int main()
{
memset(h,-1,sizeof h);//鍒濆鍖栧ご鑺傜偣锛屽叏涓?1
int a,b,c;
cin>>n>>m;
while(m--)
{
cin >> a >> b >> c;
add(a,b,c); // 鍗曞悜杈? //add(b,a,c) 鍙屽悜
}
for(int i = 0;i < n; ++i){
for(int j = h[i];j != -1;j = ne[j]){
int v = e[j];
cout << i <<" " <<v<<" "<<w[v]<<" ";
}
cout << endl;
}
return 0;
}
鍥剧殑閬嶅巻
#include <iostream>
#include <cstring>
using namespace std;
const int N = 233;
char g[N][N];
int n,m,ans;
int dx[8] = {0, 0,1,1, 1,-1,-1,-1}; // 鏂瑰悜鏁扮粍
int dy[8] = {1,-1,0,1,-1, 0, 1,-1};
bool pd(int x,int y){ // 鍒ゆ柇鏄惁瓒婄晫
if(x >= 1 && x <= m && y >= 1 && y <= n) return 1;
else return 0;
}
void dfs(int x,int y){ // dfs姹傝繛閫氬潡
for(int i = 0;i < 8; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(pd(nx,ny) && g[nx][ny] == '@'){
g[nx][ny] = '*';
dfs(nx,ny);
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
while(cin >> m >> n){
if(m == 0 && n == 0) break;
memset(g,0,sizeof g);
ans = 0;
for(int i = 1;i <= m; ++i){
for(int j = 1;j <= n; ++j)
cin >> g[i][j];
}
for(int i = 1;i <= m; ++i){
for(int j = 1;j <= n; ++j){
if(g[i][j] == '@'){
ans++; // 杩為€氬潡涓暟++
g[i][j] = '*';
dfs(i,j);
}
}
}
cout << ans << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 50;
char g[N][N];
int r,c,ans;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
bool vis[N][N]; // 鍒ゆ柇鏄惁璁块棶杩?鐐?struct Ponit{
int x,y,t; // x,y 鍧愭爣锛?t 浠h〃褰撳墠鐨勬鏁? Ponit(int x,int y,int t):x(x),y(y),t(t){} // 鏋勯€犲嚱鏁?};
bool pd(int x,int y){
if(x >= 1 && x <= r && y >= 1 && y <= c) return 1;
else return 0;
}
void bfs(){
queue<Ponit> q;
q.push({1,1,1}); // 鍒濆鍖?闃熷垪
vis[1][1] = 1;
while(q.size()){
Ponit t = q.front();
q.pop();
for(int i = 0;i < 4; ++i){
int nx = t.x + dx[i];
int ny = t.y + dy[i];
if(nx == r && ny == c) {
ans = t.t + 1;
return ;
}
if(pd(nx,ny) && g[nx][ny] == '.' && !vis[nx][ny]){
vis[nx][ny] = 1;
q.push({nx,ny,t.t + 1});
}
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> r >> c;
for(int i = 1;i <= r; ++i){
for(int j = 1;j <= c; ++j)
cin >> g[i][j];
}
bfs();
cout << ans;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int g[N][N],n,m,dist[N];
bool st[N];
// dist[i] 浠h〃 1 鍒?i鐨勬渶鐭矾
int dijkstra(){
memset(dist,0x3f,sizeof dist);
dist[1] = 0;
// 寰幆 n 娆★紝姣忔纭畾涓€涓渶鐭矾鐨勫€? for(int i = 0;i < n; ++i){
int t = -1;
// 鍦ㄦ墍鏈?st 涓?false鐨勭偣涓紝鎵惧埌 dist鏈€灏忕殑鐐? for(int j = 1;j <= n; ++j){
if(!st[j] && (t == -1 || dist[t] > dist[j])){
t = j;
}
}
st[t] = 1;
for(int j = 1;j <= n; ++j){
dist[j] = min(dist[j],dist[t] + g[t][j]);
// dist[t] + g[t][j] 琛ㄧず锛?1 - t鐨勬渶鐭矾 鍔犱笂 t 鍒?j鐨勮窛绂伙紝
// 绛変环浜?1 - j 鐨勮窛绂? }
}
if(dist[n] == 0x3f3f3f3f) return -1;
else return dist[n];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m;
memset(g,0x3f,sizeof g);
while(m--){
int a,b,c;
cin >> a >> b >> c;
g[a][b] = min(g[a][b],c); // a 鍜?b涔嬮棿鍙兘鏈夐噸杈癸紝鍙栨渶灏忕殑杈逛负鏉冨€? }
cout << dijkstra();
return 0;
}
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e5 + 233;
typedef pair<int,int> PII;
int dist[N],e[N],w[N],ne[N],h[N],idx,n,m,x,y,z;
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 dij(){
memset(dist,0x3f,sizeof dist);
priority_queue<PII,vector<PII>,greater<PII>> heap; // 鍒濆鍖栧爢
dist[1] = 0;
heap.push({0,1});
while(heap.size()){
auto t = heap.top();// log n 鎵惧埌鏈€灏忕殑璺濈
heap.pop();
int distance = t.first,ver = t.second;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver];i != -1;i = ne[i]){
int j = e[i];
if(dist[j] > distance + w[i]){
dist[j] = distance + w[i];
heap.push({dist[j],j});
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
else return dist[n];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m;
memset(h,-1,sizeof h);
while(m--){
cin >> x >> y >> z;
add(x,y,z);
}
cout << dij();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 23;
int dist[510],st[510];
int n , m;
struct edge{
int from,to,w;
}e[N];
void bf(){
memset(dist,0x3f,sizeof dist);
dist[1] = 0;
for(int i = 0;i < n-1; ++i){
for(int j = 0;j < m; ++j){
int a = e[i].from,b = e[i].to, t = e[i].w;
dist[b] = min(dist[b],dist[a] + t);
}
}
}
int main(){
cin >> n >> m;
for(int i = 0;i < m; ++i){
cin >> e[i].from >> e[i].to >> e[i].w;
}
bf();
for(int i = 1;i <= n; ++i){
cout << dist[i] <<" ";
}
return 0;;
}
dist[b] = min(dist[b],dist[a] + t); // 浼樺寲 dist a
while(闃熷垪涓嶇┖ ) , {
鍙栧嚭闃熷ご t
鍒犻櫎闃熷ご
鍒犳帀闃熷ご鐨勬爣璁? 鏇存柊閬嶅巻 t 鐨勬墍鏈夊嚭杈?{
鏇存柊姣忎釜鐐圭殑璺濈锛屽鏋滄洿鏂版垚鍔燂紝骞朵笖褰撳墠鐨勭偣娌℃湁鍏ラ槦锛? 閭d箞灏卞姞鍏ラ槦鍒? 涓€涓偣鍙兘鍏ラ槦涓€娆★紝涓嶈兘澶氭鍏ラ槦銆? }
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 23;
int dist[510],st[510];
int e[N],ne[N],w[N],h[N],idx,n,m;
int a, b,c;
void add(int a,int b,int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
void spfa(){
memset(dist,0x3f,sizeof dist);
dist[1] = 0;
st[1] = 1;
queue<int> q; // 瀛樻墍鏈夊彉灏忎簡鐨勮妭鐐? q.push(1);
while(q.size()){
int t = q.front();
q.pop();
st[t] = 0;
for(int i = h[t];i != -1; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i];
//cnt[j] = cnt[t] + 1; 鍒ゆ柇璐熺幆
//if (cnt[j] >= n) return true; 鐩存帴璺冲嚭
if(st[j] == 0){
st[j] = 1;
q.push(j);
}
}
}
}
}
int main(){
cin >> n >> m;
memset(h,-1,sizeof h);
for(int i = 0;i < m; ++i){
cin >> a >> b >> c;
add(a,b,c);
}
spfa();
for(int i = 1;i <= n; ++i){
cout << dist[i] <<" ";
}
return 0;;
}
void floyd(){
for(int k = 1;k <= n; ++i){
for(int i = 1;i <= n; ++i){
for(int j = 1;j <= n; ++j){
g[i][j] = min(g[i][j],g[i][k] + g[k][j]);
}
}
}
}
涔犻锛?/h3>
以上是关于鍥捐 鏈€鐭矾 鍩虹的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] 244. Shortest Word Distance II 鏈€鐭崟璇嶈窛绂?II
[kuangbin甯︿綘椋瀅涓撻鍥?鏈€鐭矾缁冧範 F - Wormholes 锛堝垽鏂礋鐜級
Linux绯荤粺鍩虹鐭ヨ瘑鐩綍銆佸厓鏁版嵁銆佽蒋纭摼鎺ャ€佹枃浠剁鐞嗗懡浠ゃ€乿im鏌ユ壘鏇挎崲銆乿imtab缂╄繘