Dijkstra poj1502
来源:互联网 发布:python开发环境搭建 编辑:程序博客网 时间:2024/05/16 09:43
//poj 1502 Dijkstra//#include <iostream>#include <fstream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;const int MAXN=101;const int INF=100000000;int map[MAXN][MAXN];int N;// 各数组都从下标1开始int dist[MAXN]; // 表示当前点到源点的最短路径长度int pre[MAXN]; // 记录当前点的前一个结点//int c[MAXN][MAXN]; // 记录图的两点间路径长度//int n, line; // 图的结点数和路径数// n -- n nodes// v -- the source node// dist[] -- the distance from the ith node to the source node// pre[] -- the preious node of the ith node// c[][] -- every two nodes' distancevoid Dijkstra(int n, int v, int *dist, int *pre, int c[MAXN][MAXN]){bool s[MAXN]; // 判断是否已存入该点到S集合中for(int i=1; i<=n; ++i){dist[i] = c[v][i];s[i] = 0; // 初始都未用过该点if(dist[i] == INF)pre[i] = 0;elsepre[i] = v;}dist[v] = 0;s[v] = 1;// 依次将未放入S集合的结点中,取dist[]最小值的结点,放入结合S中// 一旦S包含了所有V中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度 // 注意是从第二个节点开始,第一个为源点for(int i=2; i<=n; ++i){int tmp = INF;int u = v;// 找出当前未使用的点j的dist[j]最小值for(int j=1; j<=n; ++j)if((!s[j]) && dist[j]<tmp){u = j; // u保存当前邻接点中距离最小的点的号码tmp = dist[j];}s[u] = 1; // 表示u点已存入S集合中// 更新distfor(int j=1; j<=n; ++j)if((!s[j]) && c[u][j]<INF){int newdist = dist[u] + c[u][j];if(newdist < dist[j]){dist[j] = newdist;pre[j] = u;}}}//cout<<"-----"<<endl;//for(int i=1;i<=N;i++)//cout<<dist[i]<<" ";}void input(){char st[100];for (int i = 1; i < N; i++)for (int j = 0; j < i; j++){scanf("%s", st);if (st[0] == 'x')map[i][j] = map[j][i] = INF;elsemap[i][j] = map[j][i] = atoi(st);}}int main(){ //cout << "Hello world!" << endl; //freopen("input.txt","r",stdin); //memset(pre,0,sizeof(pre)); //int T; cin>>N;/* for(int i=1;i<N;i++) { for(int j=1;j<=i;j++) { //cin>>map[i][j]; /* scanf("%d",&T); if(T==)map[i][j]=INF; else map[i][j]=T; char str[10]; scanf("%s",&str); if(str[0]=='x') { //map[i][j]=INF; map[j][i]=map[i][j]=INF; } else map[i][j]=map[j][i]=atoi(str);//map[i][j]=atoi(str); } }*/input(); //Output map[][]/* for(int i=1;i<N;i++) { for(int j=1;j<N;j++) { cout<<map[i][j]<<" "; } cout<<endl; }*/ //init for(int i=1; i<=N; i++)dist[i] = INF; Dijkstra(N-1,0,dist,pre,map); //cout<<dist[N]<<endl;//cout<<"testing"<<endl; //int max=-1; //for(int i=1;i<=N;++i) //{ //sum+=dist[i]; //cout<<dist[i]<<" ";//if(dist[i]>max)//max=dist[i]; //} //cout<<max<<endl;int ans = 0;for (int i = 0; i < N; i++)ans = max(ans, dist[i]);cout<<ans<<endl; //fclose(stdin); return 0;}
注意 有向图中结点中,自己到自己的结点距离为0。即对角线中的元素为0,特别注意在构造邻接矩阵时。
0 0
- poj1502 dijkstra
- Dijkstra poj1502
- poj1502--Dijkstra
- 单源最短路(Dijkstra)-poj1502
- POJ1502(Dijkstra)
- POJ1502 MPI Maelstrom Dijkstra
- poj1502 MPI Maelstrom 直接Dijkstra
- poj1502 MPI Maelstrom 最短路径,dijkstra
- POJ1502 MPI Maelstrom -DIJKSTRA练习系列
- POJ1502 MPI Maelstrom(最短路dijkstra)
- POJ1502 MPI Maelstrom(dijkstra,最短路)
- POJ1502
- poj1502
- poj1502
- POJ1502
- poj1502
- poj1502
- poj1502
- IIS怎样安装与配置
- SGU 132 Another Chocolate Maniac 状压DP
- 期末编程题参考代码
- 内联函数
- 如何查看和修改Oracle数据库服务器端的字符集
- Dijkstra poj1502
- 关于数据库中的主键和外键以及unique
- poj 1088 滑雪 (状态压缩DP)
- Eclipse快捷键大全
- 第三次实现Logistic Regression(c++)_再尝试
- Win7旗舰版中的IIS配置asp.net的运行环境
- expand函数
- win7(64位)下的某些文件,用VB6开发的程序无法判断是否存在
- easyUI 行显示嵌套对象的方法