Review Dijkstra's algorithm and practice my English

来源:互联网 发布:mac复制文件到移动硬盘 编辑:程序博客网 时间:2024/06/07 09:27
/*Problem description:in a directed graph with N nodes, starting from node 1 to i (i=2, 3, ..., N) and back to 1,(assume that going from node 1 to i needs time = weight(node 1, node i))count the minimum time needed.Solution:calculate the shortest paths from 1:from 1 to i (i=2, 3, ..., n) 2:from i to 1 (i=2, 3, ..., n)for 1, just use Dijktrafor 2, just reverse the graph and transform it into situation 1*/#include<stdio.h>#include<vector>#include<queue>using namespace std;#define MAX 100011struct node{// the node of graphint num;// the number of current nodeint dist;// the distance of previous node to current node};struct cmp{// for maintaining a minimum stackbool operator()(node x, node y){return x.dist > y.dist ? true : false;}};priority_queue<node, vector<node>, cmp> myQueue;// use for dijkstra algorithm to maitain the nodes to be expandedvector<node> vList01[MAX];// use linkedlist to store a graphvector<node> vList02[MAX];// store a reversed graphint time = 0;int dist[MAX];// dist[i] means the distance from star -> iint vist[MAX];// vist[i] means whether No.i node was visted/*Dijkstra's algorithm*/void dijkstra(vector<node> vList[MAX], int n){int i;for(i=1; i<=n; i++){// init flagdist[i] = INT_MAX;vist[i] = 0;}node start;// start nodestart.num = 1;start.dist = 0;dist[1] = 0;myQueue.push(start);while(!myQueue.empty()){// expand until all nodes were addednode v = myQueue.top();myQueue.pop();if(vist[v.num] == 1)continue;vist[v.num] = 1;int len = vList[v.num].size();for(i=0; i<len; i++){node next = vList[v.num][i];// neighbouring nodesif(dist[v.num]+next.dist < dist[next.num]){// relax nodedist[next.num] = dist[v.num]+next.dist;next.dist = dist[next.num];myQueue.push(next);// add node to be expand }}}for(i=1; i<=n; i++){// count the time time += dist[i];//printf("1->%d = %d\n", i, dist[i]);}}int main(){int ti, ni, mi;int t, n, m;scanf("%d", &t);for(ti=0; ti<t; ti++){scanf("%d %d", &n, &m);for(mi=0; mi<m; mi++){int x, y, w;scanf("%d %d %d", &x, &y, &w);// build a normal graphnode v;v.num = y;v.dist = w;vList01[x].push_back(v);// build a reversed graphnode u;u.num = x;u.dist = w;vList02[y].push_back(u);}// sovletime = 0;dijkstra(vList01, n);dijkstra(vList02, n);//clear graphfor(ni=1; ni<=n; ni++){vList01[ni].clear();vList02[ni].clear();}printf("%d\n", time);}return 0;}

0 0
原创粉丝点击