patest_1003_Emergency (25)_(dijkstra+dfs)
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 2 1 2 1 5 3 0 1 1 0 2 2 0 3 1 1 2 1 2 4 1 3 4 1Sample Output
2 4
题意:每个点有点权,每条边有边权,问一个点到另一个点的最短路径(路径上的边权和)有多少条,所有最短路径中路径点权和最大为
多少。
思路:dijkstra求最短路,再dfs找条数和最大点权和。
浙大的陈越老师出的题,会包含很多情况,锻炼自己考虑完善的能力。
总结:关于图的问题,若题中没特意说明,两点之间可以有多条权值相同或不同的边(用邻接表比较好)。在这道题中,起点和终点应该可以相同。
#include<iostream> #include<cstdio> #include<cstring> #include<stdlib.h> #include<vector> using namespace std; #define N 505 #define INF 999999999 struct Eage { int v,val,next; }eage[N*N]; int head[N],cnte; void addEage(int a,int b,int v) { eage[cnte].v=b; eage[cnte].val=v; eage[cnte].next=head[a]; head[a]=cnte++; } int dist[N],cost[N],n,m,val[N]; bool vis[N]; int res,cntr; void dijkstra(int c1,int c2) { for(int i=0;i<n;i++) { cost[i]=val[i]; dist[i]=INF; vis[i]=0; } for(int i=head[c1];i!=0;i=eage[i].next) { int u=eage[i].v,v=eage[i].val; if(dist[u]>v) dist[u]=v; } vis[c1]=1; dist[c1]=0; for(int i=1;i<n;i++) { int minn=INF,u; for(int j=0;j<n;j++) if(minn>dist[j]&&vis[j]==0) { minn=dist[j]; u=j; } vis[u]=1; for(int j=head[u];j!=0;j=eage[j].next) { int v=eage[j].v,va=eage[j].val; if(dist[v]>dist[u]+va) dist[v]=dist[u]+va; } } } bool vis1[N]; void dfs(int c1,int c2,int len,int sum) { if(len>dist[c2]) return; if(c1==c2) { cntr++; res=max(res,sum); return; } for(int i=head[c1];i!=0;i=eage[i].next) { int v=eage[i].v,va=eage[i].val; if(vis1[v]==0) { vis1[v]=1; dfs(v,c2,len+va,sum+val[v]); vis1[v]=0; } } } int main() { int c1,c2; scanf("%d%d%d%d",&n,&m,&c1,&c2); for(int i=0;i<n;i++) scanf("%d",&val[i]); cnte=1; for(int i=0;i<m;i++) { int a,b,v; scanf("%d%d%d",&a,&b,&v); addEage(a,b,v); addEage(b,a,v); } if(c1==c2) { printf("1 %d\n",val[c1]); return 0; } cntr=0; dijkstra(c1,c2); res=0; vis1[c1]=1; dfs(c1,c2,0,val[c1]); printf("%d %d\n",cntr,res); return 0; } /* 4 3 3 2 1 2 0 4 0 1 1 1 2 3 2 3 6 4 4 0 3 1 2 5 7 0 1 1 0 2 1 1 3 1 2 3 1 2 3 0 1 2 3 0 1 3 0 1 2 1 0 2 */