hdu-1385-floyd记录路径

来源:互联网 发布:tor网络连接不上 编辑:程序博客网 时间:2024/06/08 12:13

Minimum Transport Cost

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10274    Accepted Submission(s): 2831


Problem Description
These are N cities in Spring country. Between each pair of cities there may be one transportation track or none. Now there is some cargo that should be delivered from one city to another. The transportation fee consists of two parts: 
The cost of the transportation on the path between these cities, and

a certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities.

You must write a program to find the route which has the minimum cost.
 

Input
First is N, number of cities. N = 0 indicates the end of input.

The data of path cost, city tax, source and destination cities are given in the input, which is of the form:

a11 a12 ... a1N
a21 a22 ... a2N
...............
aN1 aN2 ... aNN
b1 b2 ... bN

c d
e f
...
g h

where aij is the transport cost from city i to city j, aij = -1 indicates there is no direct path between city i and city j. bi represents the tax of passing through city i. And the cargo is to be delivered from city c to city d, city e to city f, ..., and g = h = -1. You must output the sequence of cities passed by and the total cost which is of the form:
 

Output
From c to d :
Path: c-->c1-->......-->ck-->d
Total cost : ......
......

From e to f :
Path: e-->e1-->..........-->ek-->f
Total cost : ......

Note: if there are more minimal paths, output the lexically smallest one. Print a blank line after each test case.

 

Sample Input
50 3 22 -1 43 0 5 -1 -122 5 0 9 20-1 -1 9 0 44 -1 20 4 05 17 8 3 11 33 52 4-1 -10
 

Sample Output
From 1 to 3 :Path: 1-->5-->4-->3Total cost : 21From 3 to 5 :Path: 3-->4-->5Total cost : 16From 2 to 4 :Path: 2-->1-->5-->4Total cost : 17
 

题目大意:

                   给你n个点和点与点的路径,然后询问多次,每次询问从u到v的最短路径,并且打印路径,如果有多条则输出字典序最的

题目思路:

                   这题可以用很多种方法做,这里用的是floyd的方法,我们可以用个数组path[i][j]表示路径i->j当前点为i的下一的点,所以初

                   始值为path[i][j] = j,而我们知道floyd更新时是i->j  -->  i->k->j 所以我们就可以让path[i][j] = path[i][k] 也就是让i->j的下一个点 

                   接到了i->k的下一个点而当值相等时,起点都是i,下一个点是 path[i][j] 和path[i][k] 而要字典序最小所以我们取个最小                                                          值,path[i][j] = min(path[i][j],path[i][k])


AC代码:

#include<iostream>#include<cstdio>#define min(x,y) (x<y?x:y)using std::cin;const int maxn = 50;const int inf = 1e8;class pathFloyd{public:    int n;    int dis[maxn][maxn],path[maxn][maxn],w[maxn];    void init(){        while(cin>>n,n){            for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){                scanf("%d",&dis[i][j]);path[i][j]=j;                if(dis[i][j]==-1)dis[i][j]=inf;            }            for(int i=1;i<=n;i++)scanf("%d",&w[i]);            floyd();            int u,v;while(cin>>u>>v,u!=-1)output(u,v);        }    }    void floyd(){        for(int k=1;k<=n;k++)            for(int i=1;i<=n;i++)if(i!=k)                for(int j=1;j<=n;j++)if(j!=i&&j!=k){                    if(dis[i][j]>dis[i][k]+dis[k][j]+w[k]){                        dis[i][j]=dis[i][k]+dis[k][j]+w[k];                        path[i][j]=path[i][k];                    }else if(dis[i][j]==dis[i][k]+dis[k][j]+w[k]){                        path[i][j]=min(path[i][j],path[i][k]);      //值相等时取最小值                     }                }    }    void output(int s,int t){        printf("From %d to %d :\nPath: %d",s,t,s);int nex=s;        while(s!=t){printf("-->%d",path[s][t]);s=path[s][t];}        printf("\nTotal cost : %d\n\n",dis[nex][t]);    }}pf;int main(){    pf.init();    return 0;}


hdu1224:数据保证路径唯一,输出路径最长的一条


AC代码:

#include<iostream>#include<cstdio>#define min(x,y) (x<y?x:y)using namespace std;const int maxn = 105;const int inf = 1e8;class pathFloyd{public:    int n;    int dis[maxn][maxn],path[maxn][maxn],w[maxn];    void init(){        int t;cin>>t;int T=1;        while(t--){            scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&w[i]);            w[n+1]=0;            int m;scanf("%d",&m);            for(int i=1;i<=n+1;i++)            for(int j=1;j<=n+1;j++){                path[i][j]=j;                dis[i][j]=-1;            }            while(m--){                int u,v;scanf("%d%d",&u,&v);                dis[u][v]=w[v];            }            floyd();            if(T>1)printf("\n");            cout<<"CASE "<<T++<<"#"<<endl;            output(1,n+1);        }    }    void floyd(){        for(int k=1;k<=n+1;k++)            for(int i=1;i<=n+1;i++)if(i!=k)                for(int j=1;j<=n+1;j++)if(j!=i&&j!=k){                    if(dis[i][j]<dis[i][k]+dis[k][j]&&dis[i][k]>=0&&dis[k][j]>=0){                        dis[i][j]=dis[i][k]+dis[k][j];                        path[i][j]=path[i][k];                    }                }    }    void output(int s,int t){        printf("points : %d\ncircuit : %d",dis[s][t],s);        int nex = path[s][t];        while(nex!=t)printf("->%d",nex),nex = path[nex][t];        printf("->1\n");    }}pf;int main(){    pf.init();    return 0;}





0 0
原创粉丝点击