每次都找最短路徑加入集合 -> 再從最短的出發
直到沒有點
類 bfs 走訪(相鄰)
PSEUDOCODE:
while ( 找到下一個點 ) :
找未訪問過(白點) 路徑最短
for(走訪 n 個點):
if (和當前點聯通):
更新路徑
時間複雜度主要來自 "提取最小值"
可以使用 priority_queue 做優化
預設最大先排 要改成最小
priority_queue< mpair,vector<mpair> ,greater<mpair>>
UVa 534
(跳青蛙 -> 求單次(兩點間)最短)
#include <iostream>
#include <queue>
#include <cmath>
#include <iomanip>
using namespace std;
typedef pair<double,double> mpair;
class point{
public:
int x,y;
double distance(point b){ return sqrt( (b.x-x)*(b.x-x) + (b.y-y)*(b.y-y)); }
};
int main()
{
int n,r=1;
while(cin>>n,n){
vector<point> stone(n);
vector<vector<double> > graph(n,vector<double>(n));
priority_queue< mpair,vector<mpair> ,greater<mpair>> djk;
vector<bool> visted(n,0);
for(int i=0;i<n;i++)
cin>>stone[i].x>>stone[i].y;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
graph[i][j] = stone[i].distance(stone[j]);
djk.push({0,0});
double mindist = -1;
while(!djk.empty()){
int np = djk.top().second;
visted[np] = true;
mindist = max(djk.top().first,mindist);
if(np == 1)
break;
djk.pop();
for(int i = 0; i <n ;i++){
if(!visted[i]){
djk.push({graph[np][i],i});
}
}
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n",r++,mindist);
}
return 0;
}
import pygame import random im pygame.init() 定義遊戲視窗大小 WIDTH = 600 HEIGHT = 200 screen = pygame.display.set_mode((WIDTH, HEIGHT))
Jun 17, 2025Pygame 教學 (一) 共筆:https://hackmd.io/LuheTD_SQwWYa2wMwiv65g 匿名提問 前情提要 我們今天要來做打磚塊! 在這之前 我們來把遊戲的流程釐清一下
Dec 2, 2021並查集 目標 判斷連通 , 找兩個點有沒有在同個集合裡面 ( 有同個father,血緣關係 ) 實作 準備一個 陣列 存根節點 並把根結點 設為自己 製作兩個 funtion
Dec 12, 2020Graph Searching [ ] BFS [ ] DFS Shortest Path [x] Dijkstra [ ] Floyd-Warshall
Oct 31, 2020or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up