- #include<bits/stdc++.h>
- //#include<iostream>
- //#include<vector>
- //#include<utility>
- //#include<algorithm>
- //#include<climits>
- using namespace std;
- int main()
- {
- int S, T;
- cin >> S >> T;
- --S;
- --T;
- vector<vector<pair<int,int>>> graph(12);
- constexpr int edges[30][2] =
- {0, 1, 0, 2, 0, 3, 0, 4, 0, 5, //1-5
- 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, //6-10
- 1, 8, 1, 7, 2, 7, 2, 6, 3, 6, //11-15
- 3, 10, 4, 10, 4, 9, 5, 9, 5, 8, //16-20
- 6, 7, 7, 8, 8, 9, 9, 10, 10, 6, //21-25
- 11, 6, 11, 7, 11, 8, 11, 9, 11, 10 //26-30
- };
- for(int i=0; i<30; ++i)
- {
- int cost;
- cin >> cost;
- graph[edges[i][0]].push_back({edges[i][1], cost});
- graph[edges[i][1]].push_back({edges[i][0], cost});
- }
- vector<int> dist(12, INT_MAX);
- vector<bool> visited(12, false);
- dist[S] = 0;
- while(!visited[T])
- {
- int min_dist = INT_MAX;
- int min_node = 0;
- for(int i=0; i<12; ++i)
- {
- if(!visited[i] && dist[i]<min_dist)
- {
- min_node = i;
- min_dist = dist[i];
- }
- }
- visited[min_node] = true;
- for(auto &p: graph[min_node])
- {
- dist[p.first] = min(dist[p.first], dist[min_node]+p.second);
- }
- }
- cout << dist[T] << endl;
- }
- /*
- 輸入範例1
- 2 12
- 1 1 1 1 1
- 1 1 1 1 1
- 1 1 1 1 1
- 1 1 1 1 1
- 1 1 1 1 1
- 1 1 1 1 1
- 輸出範例1
- 2
- 輸入範例2
- 1 12
- 1 999 999 999 999
- 1 2 3 4 999
- 999 999 999 999 999
- 999 999 999 3 999
- 3 5 999 4 1
- 999 999 2 999 999
- 輸出範例2
- 29
- */
複製代碼 |