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
- Review Dijkstra's algorithm and practice my English
- Dijkstra算法(Dijkstra's algorithm)
- Dijkstra\'s Algorithm
- Dijkstra's Algorithm
- Dijkstra's algorithm
- Dijkstra's Algorithm
- Algorithm and Data Structure Review
- 算法学习 - Dijkstra's Algorithm
- 算法学习 - Dijkstra's Algorithm
- Dijkstra's shortest path algorithm
- Dijkstra’s shortest path algorithm
- 迪克斯特拉算法-- Dijkstra's Algorithm
- Dijkstra's Algorithm(迪杰斯特拉算法)
- Bellman-Ford Algorithm and Dijkstra Algorithm
- English Practice ...
- 单源最短路Dijkstra's algorithm径
- Negative weights using Dijkstra's Algorithm
- Dijkstra's Algorithm of shortest path
- MapReduce Counter监控窗口各个参数的含义
- <table>和<tr>元素的align属性的区别
- Android自定义状态栏通知(Status Notification)的正确实现
- 为马航失踪的同胞 祈福
- git服务器之gitolite搭建
- Review Dijkstra's algorithm and practice my English
- Android Jni开发环境搭建完整版
- 抓取网页内容并保存,获取两个字符串之间的内容
- (libgdx学习)GestureDetector
- 【面试对宝典40题的解释】匿名内部类访问外部类中的局部变量必须是final属性
- Linux双网关配置问题。
- yt-2459: 爱你多少年呢
- 经典排序算法(Java版)
- 【Job】收集统计信息的job GATHER_STATS_JOB在上午十点执行