1003 Emergency
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.
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.
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.
- 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
2 4
自己写的代码:写不出来T...T(只会写算法,不会写其他的了)
还是来借鉴借鉴大佬的吧:
- #include
- #include
- using namespace std;
-
- const int N=510;
- int g[N][N],dist[N],w[N],num[N],weight[N];
- bool st[N];
- int n,m,c1,c2;
-
- void dijkstra(){
- memset(dist,0x3f,sizeof dist);
- dist[c1]=0;
- num[c1]=1;
- w[c1]=weight[c1];
-
- for(int i=0;i
- int t=-1;
-
- for(int j=0;j
- if(!st[j] && (t==-1 || dist[t]>dist[j]))
- t=j;
- }
-
- for(int j=0;j
- if(dist[j]>dist[t]+g[t][j]){
- dist[j]=dist[t]+g[t][j];
- num[j]=num[t];
- w[j]=w[t]+weight[j];
- }
- else if(dist[j]==dist[t]+g[t][j]){
- num[j]+=num[t];
- w[j]=max(w[j],w[t]+weight[j]);
- }
- }
- st[t]=true;
- }
- }
-
- int main(){
- cin >> n >> m >> c1 >> c2;
-
- for(int i=0;i
scanf("%d",&weight[i]); -
- memset(g,0x3f,sizeof g);
- for(int i=0;i
- int a,b,c;
- scanf("%d%d%d",&a,&b,&c);
- g[a][b]=g[b][a]=c;
- }
-
- dijkstra();
-
- printf("%d %d",num[c2],w[c2]);
-
- return 0;
- }
总结:dijkstra算法实质上是求起点到每一个点的情况(这些情况可以是距离(最大或者最小),可以是权值,还可以是其他的东西),因为算法会从第一个点起遍历每一个点
好好学习,天天向上!
我要考研!
-
相关阅读:
unity 获取3D物体的方向数据
Spring 全家桶 第六章:Spring MVC 实践
期货开户需要具备⼀定的条件
Word docx文件重命名为zip文件,解压后直接查看和编辑
用VirtualBox打开VMware创建的虚拟机的方法
Optional小记
Win7下设置“定时关机”的方法
【开发篇】二十三、SpringBoot Admin端点指标控制以及自定义端点
六边形架构浅析
【课程作业】西瓜书 机器学习课后习题 : 第六章
-
原文地址:https://blog.csdn.net/weixin_50679551/article/details/126589572