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 (<=500) 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 Input4 5 0 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20Sample Output
0 2 3 3 40
这一题和前面的有个很像,其关键思路就是用深度优先思路进行路线的查找然后回溯,直到找到最进的并且cost最低的那条路径。为了让深搜的效率更高,我用了dijkstra算法先把最短的路径求出来,这样在每次递归调用dfs的时候优先进行深度比较,如果大于了最短路径直接回溯。
代码如下:
#include <stdio.h>
#define PRINT_I(x) printf("%s = %d\n",#x,x);
#define PRINT_F(x) printf("%s = %f\n",#x,x);
#define PRINT_S(x) printf("%s = %s\n",#x,x);
const int INFINITY = 10000000;
int path[500][500];
int cost[500][500];
int visit[500] = {0};
int dist[500];
int min_path,min_cost,min_count,count,fdist,fcost,border;
int result[500],tmp[500];
void init(int *v,int n);
int find_min(int *v,int n,int *visit);
void dijsktra(int s,int n);
int dfs(int s,int n,int d);
int main(int argc,char* argv[])
{
int i,j,n,m,s,d;
//freopen("input","r",stdin);
scanf("%d%d%d%d",&n,&m,&s,&d);
//initial path and cost
for(i=0;i<n;i++)
for(j=0;j<n;j++)
path[i][j] = cost[i][j] = (i==j)?0:INFINITY;
for(i=0;i<m;i++)
{
int x,y,distance,mcost;
scanf("%d%d%d%d",&x,&y,&distance,&mcost);
path[x][y] = path[y][x] = distance;
cost[x][y] = cost[y][x] = mcost;
}
dijsktra(s,n);
border = dist[d];
min_path = INFINITY;
min_cost = INFINITY;
count = fcost = fdist = 0;
init(visit,n);
visit[s] = 1;
dfs(s,n,d);
printf("%d ",s);
for(i=0;i<min_count;i++)
printf("%d ",result[i]);
printf("%d %d\n",min_path,min_cost);
return 0;
}
void init(int *v,int n)
{
int i;
for(i=0;i<n;i++)
v[i] = 0;
}
int find_min(int *v,int n,int *visit)
{
int i,min,min_index;
min_index = -1;
min = INFINITY;
for(i=0;i<n;i++)
{
if(v[i]<min && !visit[i])
{
min = v[i];
min_index = i;
}
}
return min_index;
}
void dijsktra(int s,int n)
{
int i,j,min;
visit[s] = 1;
for(i=0;i<n;i++)
dist[i] = path[s][i];
min = find_min(dist,n,visit);
while(min != -1)
{
visit[min] = 1;
for(j=0;j<n;j++)
{
if(path[s][min] + path[min][j] < path[s][j])
dist[j] = path[s][min] + path[min][j];
}
min = find_min(dist,n,visit);
}
}
int dfs(int s,int n,int d)
{
int i;
if(fdist > border)
return 0;
if(s == d)
{
if( (fdist<min_path) || (fdist==min_path&&fcost<min_cost) )
{
for(i=0;i<count;i++)
result[i] = tmp[i];
min_count = count;
min_path = fdist;
min_cost = fcost;
}
else
return 0;
}
for(i=0;i<n;i++)
{
if(path[s][i]!=INFINITY && !visit[i])
{
visit[i] = 1;
fdist += path[s][i];
fcost += cost[s][i];
tmp[count++] = i;
dfs(i,n,d);
visit[i] = 0;
fdist -= path[s][i];
fcost -= cost[s][i];
count--;
}
}
return 0;
}