Created
March 3, 2016 12:37
-
-
Save hsnks100/74d5c703338205d0dda8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <queue> | |
#include <functional> | |
/* | |
* | |
* | |
* | |
7 10 | |
1 2 2 | |
1 3 5 | |
2 3 4 | |
2 4 6 | |
3 4 2 | |
2 5 10 | |
4 6 1 | |
5 6 3 | |
5 7 5 | |
6 7 9 | |
*/ | |
using namespace std; | |
struct edge{ | |
int to, cost; | |
edge(int _to, int _cost) | |
{ | |
to = _to; | |
cost = _cost; | |
} | |
}; | |
typedef pair<int, int> P; | |
int V; | |
vector<edge> G[101]; | |
int d[101]; | |
void dijkstra(int s) | |
{ | |
priority_queue<P, vector<P>, greater<P>> que; | |
fill(d, d + V + 1, numeric_limits<int>::max()); | |
d[s] = 0; | |
que.push(P(0, s)); | |
while(!que.empty()) | |
{ | |
P p = que.top(); | |
que.pop(); | |
// queList.pop_front(); | |
int v = p.second; | |
if(d[v] != p.first) // 현재 구한게 이전의 최적값이랑 다르면 필요가 없음. | |
continue; | |
for(int i=0; i<G[v].size(); i++) | |
{ | |
edge e = G[v][i]; | |
if(d[e.to] > d[v] + e.cost) | |
{ | |
d[e.to] = d[v] + e.cost; | |
que.push(P(d[e.to], e.to)); | |
} | |
} | |
} | |
} | |
int main() | |
{ | |
int edgeNumbers = 0; | |
cin >> V >> edgeNumbers; | |
for(int i=1;i <= edgeNumbers; i++) | |
{ | |
int from, to, cost; | |
cin >> from >> to >> cost; | |
edge e = edge(to, cost); | |
G[from].push_back(e); | |
swap(to, from); | |
e = edge(to, cost); | |
G[from].push_back(e); | |
} | |
dijkstra(1); // 1 ?먯꽌 ?섑뻾 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment