1003. Emergency (25)【Dijkstra or DFS】
来源:互联网 发布:java 鼠标点击事件 编辑:程序博客网 时间:2024/06/03 17:14
1003. Emergency (25)
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.
5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output
2 4
自从退役后实在是好久没有敲题,感觉代码能力、数据结构、算法都忘记了[T T]
这题求最短路径,可以用Dijkstra,也可以用DFS,但输出确是:the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. 【坑点1】
而且,测试数据有C1==C2的情况,wa了第二个数据很久很久= =【坑点2】
上代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm> using namespace std; const int maxnum = 505;const int maxint = 0x3f3f3f3f; int N,M,C1,C2;void Dijkstra(int n, int v, int *dist, int *h,int *prev, int c[maxnum][maxnum],int hands[maxnum],int *pathcount){ bool s[maxnum]; // 判断是否已存入该点到S集合中 int i; for(i=0; i<n; ++i) { dist[i] = c[v][i]; if(dist[i]==maxint) { h[i] = hands[i];pathcount[i]=0;} else { h[i] = hands[v]+hands[i]; pathcount[i] = 1;} s[i] = 0; // 初始都未用过该点 if(dist[i] == maxint) prev[i] = -1; else prev[i] = v; } dist[v] = 0; s[v] = 1; pathcount[v] = 1; // 依次将未放入S集合的结点中,取dist[]最小值的结点,放入结合S中 // 一旦S包含了所有V中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度 for(i=2; i<=n; ++i) { int tmp = maxint; int u = v; // 找出当前未使用的点j的dist[j]最小值 for(int j=0; j<n; ++j) if((!s[j]) && dist[j]<tmp) { u = j; // u保存当前邻接点中距离最小的点的号码 tmp = dist[j]; } //if(tmp==maxint||u==C2) break; s[u] = 1; // 表示u点已存入S集合中 // 更新dist for(int j=0; j<n; ++j) if((!s[j]) && c[u][j]<maxint) { int newdist = dist[u] + c[u][j]; int newh = h[u]+hands[j]; if(newdist < dist[j]) { pathcount[j] = pathcount[u]; dist[j] = newdist; h[j] = newh; prev[j] = u; } else if(newdist == dist[j]) { pathcount[j] += pathcount[u]; if(newh>h[j]) h[j] = newh;} } }}int main(){int dist[maxnum]; int prev[maxnum]; int h[maxnum]; int pathcount[maxnum];//用来记录所有最短路径的条数int hands[maxnum];int A[maxnum][maxnum];memset(hands,0,sizeof(hands));memset(A,0,sizeof(A));while(~scanf("%d%d%d%d",&N,&M,&C1,&C2)){int i,j;for(i=0;i<N;i++){for(j=0;j<N;j++){A[i][j]=maxint;}}for(i=0;i<N;i++){scanf("%d",&hands[i]);}int a,b,c;for(i=0;i<M;i++){scanf("%d%d%d",&a,&b,&c);A[a][b]=c;A[b][a]=c;}for(i=0;i<N;i++) {dist[i]=maxint;}Dijkstra(N, C1, dist, h,prev, A,hands,pathcount);printf("%d %d\n",pathcount[C2],h[C2]);}return 0;}
终于来贴DFS的代码了,看着大牛的打完,感觉吧,看完代码能理解,但是搁自己就敲不出DFS,天哪,谁来教教我!!
稍微说说对DFS的想法,首先要明确返回的条件是什么(这里是等于终点的时候),其次,如果有位置标记的话,返回的什么时候取消标记(这里是那个s数组,只是对于取消 标记是在循环外我有点蒙蔽,有人教教我就好了!先谢谢您!)
上代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm> using namespace std; const int maxnum = 505;const int maxint = 0x3f3f3f3f; int N,M,C1,C2;int A[maxnum][maxnum];int hands[maxnum];int mindist = maxint;int maxhand = 0;int cnt = 0;int s[maxnum];void dfs(int v,int dist,int hand){if(v==C2){if(dist<mindist){cnt=1;mindist = dist;maxhand = hand;}else if(dist == mindist){cnt++;if(hand>maxhand)maxhand = hand;}return;}s[v]=1;int i,j;for(i=0;i<N;i++){if((!s[i])&&A[v][i]>0)dfs(i,dist+A[v][i],hand+hands[i]);}s[v]=0;}int main(){while(~scanf("%d%d%d%d",&N,&M,&C1,&C2)){int i,j;memset(s,0,sizeof(s));for(i=0;i<N;i++){for(j=0;j<N;j++)A[i][j]=-1;}for(i=0;i<N;i++){scanf("%d",&hands[i]);}int a,b,c;for(i=0;i<M;i++){scanf("%d%d%d",&a,&b,&c);A[a][b]=c;A[b][a]=c;}dfs(C1,0,hands[C1]);printf("%d %d\n",cnt,maxhand);}return 0;}
- 1003. Emergency (25)【Dijkstra or DFS】
- 1003. Emergency (25)(Dijkstra + DFS)
- PAT 1003. Emergency (25) Dijkstra算法 + DFS
- 1003. Emergency (25) DFs
- 1003. Emergency (25)[dfs]
- 1003. Emergency (25) PAT+DFS
- PAT 甲级1003. Emergency (25) DIJKSTRA
- 1003. Emergency (25)(dijikstra+DFS)
- 1003. Emergency (25)(经典的dfs)
- 1003. Emergency (25)-PAT甲级真题(Dijkstra算法)
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- Emergency (dijkstra 变形)
- 1003. Emergency (25)
- 1003. Emergency (25)-PAT
- (PAT)1003. Emergency (25)
- 1003. Emergency (25)
- 【C++】1003. Emergency (25)*
- 1003. Emergency (25)
- 【[Offer收割]编程练习赛15-B分数调查】
- linux redhat 配置DHCP服务
- HERP和单臂路由结合配置时,提示 %IP-4-DUPADDR: Duplicate address错误
- win7安装Anaconda+TensorFlow+配置PyCharm
- C++实现稀疏矩阵的压缩存储
- 1003. Emergency (25)【Dijkstra or DFS】
- No result defined for action com.mybank.control.FundAction and result login
- 四种方案解决ScrollView嵌套ListView问题
- 学习记录 matlab里workspace与m文件如何交换数据
- 计算机系统——原码反码补码
- RecyclerView实现横向滚动
- 深入剖析C++中的三种new
- StringBuffer和StringBuilder
- 集合框架-增强for