https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376

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 Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

时间复杂度:$O(2 * N ^ 2)$

代码:

#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
int N, M, C1, C2;
int cnt = 0;
int mp[550][550];
int team[550];
int vis[550], dis[550];
int see[550];
int amount[550]; void dijkstra(int act) {
memset(vis, 0, sizeof(vis));
memset(dis, inf, sizeof(dis)); dis[act] = 0;
int temp = act;
see[act] = 1;
amount[act] = team[act]; for(int i = 0; i < N; i ++) {
dis[i] = mp[act][i];
if(dis[i] != inf && i != act) {
amount[i] = amount[act] + team[i];
see[i] = 1;
}
} for(int i = 0; i < N - 1; i ++) {
int minn = inf;
for(int j = 0; j < N; j ++) {
if(dis[j] < minn && vis[j] == 0) {
minn = dis[j];
temp = j;
}
} vis[temp] = 1;
for(int k = 0; k < N; k ++) {
if(!vis[k] && dis[k] > dis[temp] + mp[temp][k]) {
dis[k] = dis[temp] + mp[temp][k];
amount[k] = amount[temp] + team[k];
see[k] = see[temp];
}
else if(!vis[k] && dis[k] == dis[temp] + mp[temp][k]) {
see[k] += see[temp];
if (amount[k] < amount[temp]+team[k])
amount[k] = amount[temp]+team[k];
}
}
}
return;
} int main() {
scanf("%d%d%d%d", &N, &M, &C1, &C2);
for(int i = 0; i < N; i ++)
scanf("%d", &team[i]); memset(mp, inf, sizeof(mp));
for(int i = 1; i <= M; i ++) {
int a, b, cost;
scanf("%d%d%d", &a, &b, &cost);
mp[a][b] = mp[b][a] = cost;
} dijkstra(C1);
printf("%d %d\n", see[C2], amount[C2]);
return 0;
}

  

最新文章

  1. SQL Server-5种常见的约束
  2. shell crontab执行结果不同问题处理
  3. ecshop分页
  4. Android使用service后台更新计划任务
  5. sencha touch之store
  6. Biba模型简介
  7. wordCount程序中MapReduce工作过程分析
  8. Sliding Window Maximum 解答
  9. 更改Oracle数据文件名及数据文件存放路径
  10. (转)hadoop三个配置文件的参数含义说明
  11. 定制化jQuery
  12. jenkins用户权限配置错误,导致登录时提示:没有Overall/read权限
  13. Python爬虫——request实例:爬取网易云音乐华语男歌手top10歌曲
  14. 全志A33开发板的安卓控制LED-2-JNI基础
  15. c++ A类包含B类指针,B类包含A类指针的情况
  16. PPTP不使用远程网关访问公网设置
  17. NOI 7614 最低通行费(多段图最短路)
  18. GIT与VCS
  19. pandas replace函数使用小结
  20. exe4j生成的exe反编译成java代码

热门文章

  1. Swift 中关于”??”操作符
  2. Webpack Tapable原理详解
  3. [异常笔记]required a bean of type &#39;org.quartz.JobExecutionContext&#39; that could not be found
  4. JS数组push一个对象
  5. Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO
  6. [BZOJ1455]罗马游戏(左偏树)
  7. HDU暑假多校第六场K-werewolf
  8. stm32--FatFs调试过程(SPIFlash)
  9. Vue-router用法
  10. asp.net 模拟CURL调用微信公共平台API 上传下载多媒体文件接口