1003. Emergency (25):简单最短路径问题
来源:互联网 发布:围巾淘宝加盟 编辑:程序博客网 时间:2024/05/19 17:06
#include<iostream>#include<fstream>using namespace std;#define INFINITY 21474843649#define MaxSize 500int Map[MaxSize][MaxSize]; // 用邻接矩阵表示带权图int dist[MaxSize]; // dist[i]存放顶点i的当前最短路径长度int pathCount[MaxSize]; // the number of different shortest pathsint N, M, C1, C2;int teams[MaxSize]; // teams[i] is the number of rescue teams in the i-th citbool visited[MaxSize];int amount[MaxSize]; // the amount of rescue teams you can possibly gather.void Dijkstra( int Start ){ dist[Start] = 0; visited[Start]= true; amount[Start] = teams[Start]; int Vertex = Start; while( Vertex!=C2 ) { for( int i=0; i<N; i++ ) { if( !visited[i] ) { if( dist[i] > dist[Vertex]+Map[Vertex][i] ) { dist[i] = dist[Vertex]+Map[Vertex][i]; amount[i] = amount[Vertex] + teams[i]; pathCount[i] = pathCount[Vertex]; } else if( dist[i] == dist[Vertex]+Map[Vertex][i] ) // 当前最短路径 不止一条 { pathCount[i] += pathCount[Vertex]; if( amount[i] < amount[Vertex] + teams[i] ) // 更新此刻救援队的最大数量 amount[i] = amount[Vertex] + teams[i]; } } } int minDist = INFINITY; for( int i=0; i<N; i++ ) if( !visited[i] && dist[i] < minDist ) // 更新 下一个要加入 最短路径中的 顶点 { minDist = dist[i]; Vertex = i; } visited[Vertex] = true; }}int main(){ //ifstream cin("test.txt"); cin>>N>>M>>C1>>C2; int c1, c2, L; for( int i=0; i<N; i++ ) cin>>teams[i]; for( int i=0; i<N; i++ ) { dist[i] = INFINITY; pathCount[i] = 1; //at least one path from C1 to C2 visited[i] = false; for( int j=0; j<N; j++ ) Map[i][j]=INFINITY; } for( int i=0; i<M; i++ ) { cin>>c1>>c2>>L; Map[c1][c2] = Map[c2][c1] = L; } Dijkstra(C1); cout<<pathCount[C2]<<" "<<amount[C2]<<endl; return 0;}
0 0
- 1003. Emergency (25):简单最短路径问题
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- PAT 1003. Emergency (25)<最短路径,最大搜救人数>
- 【PAT】1003. Emergency (25) DFS 最短路径及最短路径数
- 街区最短路径问题-简单数论
- 1003. Emergency (25) 最短路问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 最短路径问题
- 《机器学习实战》-朴素贝叶斯笔记
- Java设计模式--适配器模式
- 每日一得-开始学习手机web开发
- pom.xml第一行报错
- 面试题
- 1003. Emergency (25):简单最短路径问题
- Leetcode[12-13] Roman to Integer & Integer to Roman(Java)
- Spring发送邮件的方式
- C++ Time Delay
- hdu 1849 (尼姆博弈)
- 游戏评测(随笔)
- ASCII码排序
- Codeforces Round #306 (Div. 2) A B C
- UILabel基础语法