李珮璠
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 4/13 科培第九次社課 ## 目錄 [TOC] --- ## 一點點圖論 ### 概念 * 濫觴:七橋問題 * 把圖簡化成點和邊 ### 類別 * 有向圖v.s.無向圖 * 權重 ### 怎麼表示一個圖呢? **先來個圖舉例** ![新增副標題 (1)](https://hackmd.io/_uploads/BJDrTENg0.png) **我是陣列:** 1表示有連通,0表示沒有連通( / ∞ ) | X | 0 | 1 | 2 | 3 | 4 | | - | - | - | - | - | - | | **0** | 0 | 1 | 1 | 0 | 0 | | **1** | 1 | 0 | 1 | 1 | 0 | | **2** | 1 | 1 | 0 | 1 | 0 | | **3** | 0 | 1 | 1 | 0 | 1 | | **4** | 0 | 0 | 0 | 1 | 0 | **矩陣v.s.串列** :::info **鄰接矩陣**:簡單方便易上手,缺點是效率低(你會開一個很巨大的陣列) **鄰接串列**:效率比較高,常用 vector 或 linked-list 實作 ::: --- ## 最短路徑 ### 概念 當我們想要求出兩地點之間的最短距離時,就需要特定的演算法幫我們求出來。 **這次我們加入邊長來看** ![新增副標題 (1)](https://hackmd.io/_uploads/BJivsNVeA.png) ### **Dijkstra**:單點到所有點的最短路徑 *~ 一種greedy的演算法* **我們先畫個表格(以1為起點)** 自己到自己視為0 | 回合 | 1 | 2 | 3 | 4 | 5 | | - | - | - | - | - | - | | 0 | 0 | 2 | 5 | X | X | | 1 | 0 | 2 | <font color="#f00">3</font> | 6 (7) | X | | 2 | 0 | 2 | 3 | <font color="#f00">5</font> | 12 (13) | | 3 | 0 | 2 | 3 | 5 | <font color="#f00">11</font> | * 不能有負權 ### 1. 陣列版 * AP325 261 :::spoiler <font color="#00FF25">我的第一個dijkstra程式</font> ```cpp= #include <iostream> using namespace std; //graph, distance,findornot int G[101][101], D[101], V[101], n; int main() { int m, i, j, k, a, b, l; cin >> n >> m; //initialize for (i=1; i<=n; i++) for (j=1; j<=n; j++) { if (i==j) G[i][j]=0;//self-self else G[i][j]=1000000000; } for (i=0; i<m; i++) { cin >> a >> b >> l;//node-node G[a][b]=l; G[b][a]=l; } //dijkstra for (i=0; i<=n; i++) D[i]=1000000000, V[i]=0; D[1]=0;//start from 1 while (1) { for (i=1, k=0; i<=n; i++){ if (V[i]==0 && D[i]<D[k]) k=i;//有連通但還沒找完 } if (k==0) break; V[k]=1; for (i=1; i<=n; i++) { if (D[k]+G[k][i]<D[i]) D[i]=D[k]+G[k][i]; } } cout<< "start from node 1"<<'\n'; for (i=1; i<=n; i++) cout << "1->"<< i << " : " << D[i] << "\n"; return 0; } ``` ::: ### 2. priority_queue版 **priority_queue介紹** * vector 或 deque 變形,預設是最大的優先取出 * 功能、語法介紹 https://sites.google.com/view/zsgititit/home/jin-jiec-cheng-shi-she-ji/stl/priority **進入正題** * AP325 262 263 https://drive.google.com/file/d/1hX7q3wVKkLuoMhEEm7uuLjq4BuhZAEgn/view?usp=sharing https://sites.google.com/view/zsgititit/home/jin-jiec-cheng-shi-she-ji-2/tu-xing-zui-duan-lu-jing :::spoiler <font color="#00FF25">PQ版本dijkstra</font> ```cpp= #include <iostream> #include <queue> #include <cstring> #include <deque> using namespace std; //struct struct Edge{ int from,to; int w; bool operator<(const Edge &rhs) const{ return (rhs.w < w); } }; deque<Edge> G[100]; bool v[100]; int dis[100]; int main(){ int n,m,a,b,w; priority_queue<Edge> pq; Edge tmp,pqedge; cin >> n >> m; for(int i=0;i<n;i++){ G[i].clear(); } for(int i=0;i<m;i++){ cin >> a >> b >> w; tmp.from=a; tmp.to=b; tmp.w=w; G[a].push_back(tmp); tmp.from=b; tmp.to=a; tmp.w=w; G[b].push_back(tmp); } memset(v,0,sizeof(v)); memset(dis,0x6f,sizeof(dis)); pqedge.from=0; pqedge.w=0; dis[0]=0; pq.push(pqedge); while(!pq.empty()){ pqedge=pq.top(); pq.pop(); int from=pqedge.from; if (v[from]==0){ v[from]=1; for(int i=0;i<G[from].size();i++){ if (v[G[from][i].to]==0){ if (dis[G[from][i].to]>dis[from]+G[from][i].w) { dis[G[from][i].to]=dis[from]+G[from][i].w; tmp.from=G[from][i].to; tmp.w=dis[G[from][i].to]; pq.push(tmp); } } } } } for(int i=0;i<n;i++){ cout << dis[i]<<" "; } cout << endl; } ``` ::: ### **Floyd-Warshall**:所有點之間的最短路徑 *~ 一種DP的演算法* * 主要想法 https://sites.google.com/view/zsgititit/home/jin-jiec-cheng-shi-she-ji-2/tu-xing-zui-duan-lu-jing #### 陣列版 :::spoiler <font color="#00FF25">我的第一個floyd-warshall程式</font> ```cpp= #include <iostream> using namespace std; int G[101][101], n; int main() { int m, i, j, k, a, b, l; cin >> n >> m; //initialize for (i=1; i<=n; i++) for (j=1; j<=n; j++) { if (i==j) G[i][j]=0; else G[i][j]=1000000000; } for (i=0; i<m; i++) { cin >> a >> b >> l; G[a][b]=l; G[b][a]=l; } //floydwarshall for (k=1; k<=n; k++) { for (i=1; i<=n; i++) { if (G[i][k]==1000000000) continue; for (j=1; j<=n; j++) { if (G[i][k]+G[k][j]<G[i][j]) G[i][j]=G[i][k]+G[k][j];//important! } } } for (i=1; i<=n; i++) { for (j=1; j<=n; j++) { cout << G[i][j] << " "; } cout << endl; } return 0; } ``` ::: --- ### 實作環節(from AP325) ![螢幕擷取畫面 2024-04-09 094144](https://hackmd.io/_uploads/Hkx1pfGg0.png) --- ## 練習資源 ### Dijkstra **ZJ d793 Number Maze** https://zerojudge.tw/ShowProblem?problemid=d793 ### Floyd-Warshall **ZJ c125 Frogger**(座標) https://zerojudge.tw/ShowProblem?problemid=c125 **ZJ c128 Heavy Cargo**(需處理字串) https://zerojudge.tw/ShowProblem?problemid=c128 **ZJ d282 11015 - 05-2 Rendezvous**(需處理字串) https://zerojudge.tw/ShowProblem?problemid=d282 ### Reference **ZJ APCS 蓋步道(實作第4題)** https://zerojudge.tw/ShowProblem?problemid=j125 **Yui Huang題目單** https://yuihuang.com/oj-level-4 --- ## 參考資料 * 黃建庭老師教學網站 演算法一 https://sites.google.com/view/zsgititit/home/jin-jiec-cheng-shi-she-ji?authuser=0 演算法二 https://sites.google.com/view/zsgititit/home/jin-jiec-cheng-shi-she-ji-2?authuser=0 * 吳邦一教授AP325講義 https://drive.google.com/file/d/1hX7q3wVKkLuoMhEEm7uuLjq4BuhZAEgn/view?usp=sharing * CSES book.pdf(只有英文版) https://cses.fi/book/book.pdf * 旭喨老師的培訓教材 * APCSC教材

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully