Dijkstra算法
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 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, 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 Specification:
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
#include <cstdio> #include <algorithm> using namespace std; const int MAXN=510; const int INF=1000000000; int n,m,c1,c2; int g[MAXN][MAXN]; bool vis[MAXN]={false}; int d[MAXN]; int weight[MAXN]; int w[MAXN]={0},num[MAXN]={0}; void dijk(int s) { fill(d,d+MAXN,INF); d[s]=0;num[s]=1;w[s]=weight[s]; for(int i=0;i<n;i++) { int u=-1,minl=INF; for(int j=0;j<n;j++) { if(vis[j]==false&&d[j]<minl) { u=j; minl=d[j]; } } if(u==-1) return; vis[u]=true; for(int j=0;j<n;j++) { if(vis[j]==false&&g[u][j]!=INF) { if(d[u]+g[u][j]<d[j]) { d[j]=d[u]+g[u][j]; w[j]=w[u]+weight[j]; num[j]=num[u]; }else if(d[u]+g[u][j]==d[j]) { num[j]+=num[u]; if(w[u]+weight[j]>w[j]) w[j]=w[u]+weight[j]; } } } } } int main() { scanf("%d%d%d%d",&n,&m,&c1,&c2); fill(g[0],g[0]+MAXN*MAXN,INF); for(int i=0;i<n;i++) scanf("%d",&weight[i]); int x,y,l; for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&l); g[x][y]=g[y][x]=l; } dijk(c1); printf("%d %d",num[c2],w[c2]); return 0; }
一套求最短路长,在更新最短距离的地方添加修改:点权,边权,数量。
1030 Travel Plan (30 分)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input:
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output:
0 2 3 3 40
#include <cstdio> #include <algorithm> #include <vector> using namespace std; const int MAXN=510; const int INF=1000000000; int n,m,st,en; int g[MAXN][MAXN]; int d[MAXN]; bool vis[MAXN]={false}; vector<int> pre[MAXN]; void dijk(int s) { fill(d,d+MAXN,INF); d[s]=0; for(int i=0;i<n;i++) { int u=-1,minl=INF; for(int j=0;j<n;j++) { if(vis[j]==false&&d[j]<minl) { u=j; minl=d[j]; } } if(u==-1) return; vis[u]=true; for(int v=0;v<n;v++) { if(vis[v]==false&&g[u][v]!=INF) { if(d[v]>d[u]+g[u][v]) { d[v]=d[u]+g[u][v]; pre[v].clear(); pre[v].push_back(u); }else if(d[v]==d[u]+g[u][v]) { pre[v].push_back(u); } } } } } int mincost=INF,c[MAXN][MAXN]; vector<int> temp,ans; void dfs(int v) { if(v==st) { temp.push_back(v); int sumcost=0; for(int i=temp.size()-1;i>0;i--) { sumcost+=c[temp[i]][temp[i-1]]; } if(sumcost<mincost) { mincost=sumcost; ans=temp; } temp.pop_back(); return; } temp.push_back(v); for(int i=0;i<pre[v].size();i++) { dfs(pre[v][i]); } temp.pop_back(); } int main() { int x,y,dis,cos; fill(g[0],g[0]+MAXN*MAXN,INF); fill(c[0],c[0]+MAXN*MAXN,INF); scanf("%d%d%d%d",&n,&m,&st,&en); for(int i=0;i<m;i++) { scanf("%d%d%d%d",&x,&y,&dis,&cos); g[x][y]=g[y][x]=dis; c[x][y]=c[y][x]=cos; } dijk(st); dfs(en); for(int i=ans.size()-1;i>=0;i--) { printf("%d ",ans[i]); } printf("%d %d",d[en],mincost); return 0; }
由于距离最短,价格最少的确定唯一,可直接Dijkstra+数组存放前驱点即为最优解。还可以用更通用的方法,先求出最短距离,并用向量存下到每一个点的最短路径,由于可能不唯一所以用多维向量。然后DFS找出价格最少的。若最佳的不唯一,也可以用向量储存。
主要的还是套路。
1018 Public Bike Management (30 分)
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.
When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.
The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3, we have 2 different shortest paths:
-
PBMC -> S1 -> S3. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3, so that both stations will be in perfect conditions.
-
PBMC -> S2 -> S3. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax (≤), always an even number, is the maximum capacity of each station; N (≤), the total number of stations; Sp, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci (,) where each Ci is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: Si, Sj, and Tij which describe the time Tij taken to move betwen stations Si and Sj. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp is adjusted to perfect.
Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.
Sample Input:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
Sample Output:
3 0->2->3 0
#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int MAXN=510; const int INF=100000000; int cm,n,sp,m; int c[MAXN]; int g[MAXN][MAXN]; int d[MAXN]; bool vis[MAXN]={false}; vector <int> pre[MAXN]; void dijk(int s) { fill(d,d+MAXN,INF); d[s]=0; for(int i=0;i<=n;i++) { int u=-1,minl=INF; for(int j=0;j<=n;j++) { if(vis[j]==false&&d[j]<minl) { u=j;minl=d[j]; } } if(u==-1) return; vis[u]=true; for(int v=0;v<=n;v++) { if(vis[v]==false&&g[u][v]!=INF) { if(d[u]+g[u][v]<d[v]) { d[v]=d[u]+g[u][v]; pre[v].clear(); pre[v].push_back(u); }else if(d[v]==d[u]+g[u][v]) { pre[v].push_back(u); } } } } } vector<int> temp,ans; int minneed=INF,minremain=INF; void dfs(int v) { if(v==0) { int need=0,remain=0; for(int i=temp.size()-1;i>=0;i--) { if(c[temp[i]]>0) remain+=c[temp[i]]; else { if(remain>abs(c[temp[i]])) remain+=c[temp[i]]; else { need+=abs(c[temp[i]])-remain; remain=0; } } } if(need<minneed) { ans=temp; minneed=need; minremain=remain; }else if(need==minneed&&remain<minremain) { minremain=remain; ans=temp; } return; } temp.push_back(v); for(int i=0;i<pre[v].size();i++) { dfs(pre[v][i]); } temp.pop_back(); } int main() { scanf("%d%d%d%d",&cm,&n,&sp,&m); c[0]=0;cm/=2; for(int i=1;i<=n;i++) { scanf("%d",&c[i]);c[i]-=cm; } fill(g[0],g[0]+MAXN*MAXN,INF); int x,y,z; for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&z); g[y][x]=g[x][y]=z; } dijk(0); dfs(sp); printf("%d ",-minneed); printf("0"); for(int i=ans.size()-1;i>=0;i--) { printf("->%d",ans[i]); } printf(" %d",minremain); return 0; }
同样是Dijkstra+DFS,套路的部分就不多说,最初以为可以通过统计路径上所有的点权之和来确定需要补还是需要取回,而根据实际情况,只从数量守恒上考虑是有漏洞的,因为必须在去的一趟里把所有的点都恢复正常,然后剩下的再带回来。如果是一来一回修复所有点的话,应该就可以直接算总数。这样实际上是在带过去的最少的情况下,到目的地时剩余的也最少,两级的选择条件。