Skip to content

Instantly share code, notes, and snippets.

@hsnks100
Created March 3, 2016 07:14
Show Gist options
  • Save hsnks100/da1af1118c1c7bd61b32 to your computer and use it in GitHub Desktop.
Save hsnks100/da1af1118c1c7bd61b32 to your computer and use it in GitHub Desktop.
#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)
{
deque<P> queList;
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d + V, numeric_limits<int>::max());
d[s] = 0;
que.push(P(0, s));
queList.push_back(P(0, s));
while(!que.empty())
{
P p = que.top();
que.pop();
for(P qq : queList)
{
printf("%d | %c \n", qq.first, 'A' - 1 + qq.second);
}
printf("pop!!\n");
queList.erase(find(queList.begin(), queList.end(), p));
// 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));
queList.push_back(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(cost, to);
G[from].push_back(e);
swap(to, from);
e = edge(cost, to);
G[from].push_back(e);
}
// for(int i=1; i<=v; i++) {
// for(edge e : g[i])
// {
// printf("%d -> %d : %d\n", i, e.to, e.cost);
// }
// }
dijkstra(1); // 1 ?먯꽌 ?섑뻾
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment