1003. Emergency (25):简单最短路径问题
2015-06-06 11:11
369 查看
#include<iostream> #include<fstream> using namespace std; #define INFINITY 21474843649 #define MaxSize 500 int Map[MaxSize][MaxSize]; // 用邻接矩阵表示带权图 int dist[MaxSize]; // dist[i]存放顶点i的当前最短路径长度 int pathCount[MaxSize]; // the number of different shortest paths int N, M, C1, C2; int teams[MaxSize]; // teams[i] is the number of rescue teams in the i-th cit bool 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; }
相关文章推荐
- Codeforces Round #306 (Div. 2) A B C
- CAP原理
- TAT,我的LCT转双旋了
- Leetcode[12-13] Roman to Integer & Integer to Roman(Java)
- arduino小车马达原理
- hdu 1849 (尼姆博弈)
- Shell awk 求标准差
- 【转】 linux内核移植和网卡驱动(二)
- 理解String不可变
- CSS Padding(填充)
- pom.xml第一行报错
- UIImage基础语法
- 软件概要设计与详细设计的区别
- 将CorelDRAW文件转成PSD分层文件详解
- 【转】 linux内核移植和驱动添加(三)
- JavaScript中String.match()方法的使用详解
- DEV C++使用说明
- setRestoreOriginalFrame
- 《机器学习实战》-朴素贝叶斯笔记
- LightOJ1011---Marriage Ceremonies (状压dp)