您的位置:首页 > 其它

1030. Travel Plan (30)

2015-08-26 21:25 218 查看
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 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 <iostream>
#include <vector>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define MAX 1010
#define INF 0x7fffffff

int N,M,S,D;
int dist[MAX][MAX],cost[MAX][MAX];
int mydist[MAX],mycost[MAX];
int path[MAX];

void myinit(){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
dist[i][j]=cost[i][j]=MAX;
}
mydist[i]=mycost[i]=MAX;
path[i]=0;
}

for(int i=0; i<M; i++){
int c1,c2,d,c;
cin>>c1>>c2>>d>>c;
dist[c1][c2]=dist[c2][c1]=d;
cost[c1][c2]=cost[c2][c1]=c;
}
}

void dij(int s){
mydist[s]=0;
mycost[s]=0;
for(int t=0; t<N; t++){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(mydist[i]!=MAX){
if(mydist[i] + dist[i][j]<mydist[j]){
mydist[j]=mydist[i]+dist[i][j];
path[i]=j;
}else if(mydist[i]+dist[i][j]==mydist[j] && mycost[i]!=MAX && mycost[i]+cost[i][j]<mycost[j]){
mycost[j]=mycost[i]+cost[i][j];
path[i]=j;
}
}
}
}
}
cout<<s<<" ";
int end=s;
while(end != D){
cout<<path[end]<<" ";
end=path[end];
}
cout<<mydist[D]<<" "<<mycost[D]<<endl;
}

int main(int argc, char** argv) {
cin>>N>>M>>S>>D;
myinit();
dij(S);

return 0;
}


结果是,



显然是因为自己没有更显cost的数值。

在孙大神那里学到了一些知识,是关于vector的应用函数的,自己平时没有怎么用过这些,所以这里就学习一下他。

#include <iostream>
#include <vector>
#include <stack>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#define MAX 1010
#define INF 0x7fffffff

struct node{
int dis,cost,path;
node(){
dis=INF;cost=INF;path=-1;
}
};
struct edge{
int end,dis,cost;
edge(int e, int d, int c){
end=e;dis=d;cost=c;
}
};

int N,M,S,D;
int dist[MAX][MAX],cost[MAX][MAX];
int mydist[MAX],mycost[MAX];

vector<vector<edge> > edg;
vector<int> path;
vector<node> city;
vector<bool> visited;

void myinit(){
edg.clear();
edg.resize(N);

for(int i=0; i<M; i++){
int c1,c2,d,c;
cin>>c1>>c2>>d>>c;
edg[c1].push_back(edge(c2,d,c));
edg[c2].push_back(edge(c1,d,c));

dist[c1][c2]=dist[c2][c1]=d;
cost[c1][c2]=cost[c2][c1]=c;
}
}

int findmin(){
int nmin=INF;
int k=-1;
for(int i=0; i<N; i++){
if(!visited[i] && city[i].dis<nmin){
nmin=city[i].dis;
k=i;
}
}
return k;
}

void dij(int s){
visited.assign(N,false);
city.clear();
city.resize(N);
city[s].dis=0;city[s].cost=0;

while(1){
int u=findmin();
if(u==-1) break;
visited[u]=true;
for(int i=0; i<edg[u].size(); i++){
int v=edg[u][i].end;
int cost=edg[u][i].cost;
int dis=edg[u][i].dis;
if(!visited[v]){
if(city[v].dis>city[u].dis+dis){
city[v].dis=city[u].dis+dis;
city[v].cost=city[u].cost+cost;
city[v].path=u;
}else if(city[v].dis==city[u].dis+dis && city[v].cost>city[u].cost+cost){
city[v].cost=city[u].cost+cost;
city[v].path=u;
}
}
}
}

stack<int> ans;
ans.push(D);
int dd=D;
while(city[dd].path!=-1){
dd=city[dd].path;
ans.push(dd);
}
while(!ans.empty()){
cout<<ans.top()<<" ";
ans.pop();
}

cout<<city[D].dis<<" "<<city[D].cost<<endl;
}

int main(int argc, char** argv) {
cin>>N>>M>>S>>D;
myinit();
dij(S);

return 0;
}


主要是vector的操作函数。;。。。。

______clear();

______resize(N);

______assign(N,false);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: