PAT (Advanced level) 1003. Emergency (25) Dijkstra
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4 题意:求从C1 到 C2的最短路的方案数及最大权值。
题解:dijkstra。当距离相同时记得叠加方案数。
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <string.h>
#define MAXX 100010
#define MMF(x) memset(x, 0, sizeof(x))
#define MMI(x) memset(x, INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = ; struct sion
{
int dic;
int amt;
int pcnt;
}C[N];
int mp[N][N];
bool vis[N];
int team[N]; void init(int &n)
{
MMF(vis);
for(int i = ; i < n; i++)
{
C[i].dic = INF;
C[i].pcnt = ;
C[i].amt = ;
for(int j = ; j < n; j++)
mp[i][j] = INF;
} }
void dijkstra(int &s, int &n)
{
queue<int>q;
vis[s] = ;
C[s].dic = ;
C[s].amt = team[s];
q.push(s);
//
while(!q.empty())
{
//cout << "~";
int now = q.front();
q.pop();
for(int i = ; i < n; i++)
{
if(!vis[i])
{ if(C[i].dic > C[now].dic + mp[now][i])
{
C[i].dic = C[now].dic + mp[now][i];
C[i].amt = C[now].amt + team[i];
C[i].pcnt = C[now].pcnt;
}
else if(C[i].dic == C[now].dic + mp[now][i])
{
C[i].pcnt += C[now].pcnt;
if(C[i].amt < C[now].amt + team[i])
C[i].amt = C[now].amt + team[i];
}
}
}
int mi = INF;
int x;
for(int i = ; i < n; i++)
{
if(!vis[i] && C[i].dic < mi)
{
mi = C[i].dic;
x = i;
}
//cout << C[i].dic << endl;
}
if(mi == INF)
break;
q.push(x);
vis[x] = ;
}
return ;
} int main()
{
int n, m, s, t;
int x, y, z;
scanf("%d%d%d%d", &n, &m, &s, &t);
for(int i = ; i < n; i++)
scanf("%d", &team[i]);
init(n);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &x, &y, &z);
if(mp[x][y] >= z)
mp[x][y] = mp[y][x] = z;
}
dijkstra(s, n); printf("%d %d\n", C[t].pcnt, C[t].amt); }
PAT (Advanced level) 1003. Emergency (25) Dijkstra的更多相关文章
- PAT (Advanced Level) 1003. Emergency (25)
最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostrea ...
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- PAT Advanced 1003 Emergency (25) [Dijkstra算法]
题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...
- PTA (Advanced Level) 1003 Emergency
Emergency As an emergency rescue team leader of a city, you are given a special map of your country. ...
- PAT (Advanced Level) 1078. Hashing (25)
二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
- PAT (Advanced Level) 1029. Median (25)
scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #i ...
- PAT (Advanced Level) 1010. Radix (25)
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
- PAT (Advanced Level) 1032. Sharing (25)
简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...
随机推荐
- 2.hbase原理(未完待续)
hbase简介相关概念hmsterhregionserver表regionhstorememstorestorefilehfileblockcacheWALminorcompactmajorcompa ...
- centos环境配置(nginx,node.js,mysql)
1.安装 Install GCC and Development Tools on a CentOS yum group install "Development Tools" n ...
- es6从零学习(二):promise
es6从零学习(二):promise 一:promise的由来 某些情况下,回调嵌套很多时,代码就会非常繁琐,会给我们的编程带来很多的麻烦,这种情况俗称——回调地狱.由此,Promise的概念就由社区 ...
- 自测之Lesson9:时钟与信号
题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...
- 【转】C++后台开发之我见
工作也快两年了,偶然看到自己以前写过的一些技术博客,发现自己自毕业后一直没有更新过自己的技术博客,趁现在是刚过完春节快要回公司工作之际,谈谈我个人对后台开发的一些个人见解,希望能够对在校的学生或者刚刚 ...
- 算法与数据结构5.2 Bubble Sort
★实验任务 给定一个 1~N 的排列 P,即 1 到 N 中的每个数在 P 都只出现一次. 现在要 对排列 P 进行冒泡排序,代码如下: for (int i = 1; i <= N; ++i) ...
- 哈希表 STL map
计数排序时我们使用一个数组来记录出现的数字的次数,而当数据范围太大时,无法建立一个那么大地数组(而且可能空间利用率很低,太浪费),此时可以改用hash table . binary search tr ...
- java — 重载和覆盖
重载(overload):对于类的方法,方法名相同,参数列表不同的方法之间构成了重载关系. 参数列表:参数的类型.参数的个数.参数的顺序. 子类从父类继承来的方法也可以发生重载. 如果多个方法有相同的 ...
- Unity3d学习日记(二)
跟着教程做让背景可以滚动起来并添加了背景的粒子特效,加入了敌机. ctrl攻击,↑↓←→移动,Game Over后按R重新开始游戏. Space Shooter游戏地址:http://ya ...
- Vue脚手架开发使用sass
vue默认采用的是原生的css,如果想要使用css预编译工具,比如sass,需要下载对应的scss的loader, 具体是 npm install --save-dev sass-loader npm ...