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 (<=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
网上看大神的做法是直接在dijistra算法上添加cost择优。与1018 Public bike不太一样,那道题涉及出来和回去两个方向,实质是两条路,所以需要存起来。贴一个自己版本的和一个网上看到比较好的版本。
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 1000000
using namespace std;
struct NODE
{
int dis;
int cost;
NODE():dis(inf),cost(inf){};
};
int mincost = inf;
vector<int>bestway;
vector<vector<int>>pre;
vector<vector<NODE>>graph;
vector<int>nowway;
void DFS(int i,int nowcost)
{
nowway.push_back(i);
if (pre[i][0] == -1)
{
if (nowcost < mincost)
{
bestway = nowway;
mincost = nowcost;
}
return;
}
else
{
for (int j = 0; j < pre[i].size(); j++)
{
//nowcost += graph[i][pre[i][j]].cost;
DFS(pre[i][j], nowcost+ graph[i][pre[i][j]].cost);
nowway.pop_back();
}
}
}
int main()
{
//ifstream fin("Text.txt");
int city, road, start, des;
cin >> city >> road >> start >> des;
//struct NODE graph[500][500];
pre.resize(city);
graph.resize(city);
int i,j,a,b,x,y;
for (i = 0; i < city; i++)
graph[i].resize(city);
//vector<int>pre(city,-1);
vector<int>dis(city, inf);
vector<int>fee(city, inf);
vector<int>flag(city, 0);
vector<int>list;
for (i = 0; i < road; i++)
{
cin >> a >> b>>x>>y;
graph[a][b].dis = x;
graph[a][b].cost = y;
graph[b][a].dis = x;
graph[b][a].cost = y;
}
for (i = 0; i < city; i++)
{
graph[i][i].cost = 0;
graph[i][i].dis = 0;
}
for (i = 0; i < city; i++)
{
dis[i] = graph[start][i].dis;
fee[i] = graph[start][i].cost;
}
pre[start].push_back(-1);
int index=-1,min=inf;
while (1)
{
index = -1, min = inf;
list.clear();
for (i = 0; i < city; i++)
{
if (flag[i] == 0 && dis[i] < min)
{
min = dis[i];
index = i;
list.clear();
list.push_back(i);
}
else if (flag[i] == 0 && dis[i] == min)
{
list.push_back(i);
}
}
if (list.empty())break;
for (i = 0; i < list.size(); i++)
{
index = list[i];
flag[index]=1;
for ( j = 0; j < city; j++)
{
if ( graph[index][j].dis < inf && flag[j] == 0)
{
if (dis[index] + graph[index][j].dis < dis[j])
{
dis[j] = dis[index] + graph[index][j].dis
;
pre[j].clear();
pre[j].push_back(index);
}
else if (dis[index] + graph[index][j].dis == dis[j])
{
pre[j].push_back(index);
}
}
}
}
}
//DFS
//vector<int>nowway;
DFS(des,0);
//cout << start;
for (i = bestway.size() - 1; i >= 0; i--)
cout<< bestway[i]<<" ";
cout << bestway.size() << " " << mincost;
//cin.get();
return 0;
}
/*2015.7.23cyq*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
//ifstream fin("case1.txt");
//#define cin fin
const int MAX=2147483647;
struct city{
int num;
int distance;
int cost;
city(int n,int td,int tc):num(n),distance(td),cost(tc){}
bool operator < (const city &a)const{
if(distance<a.distance)
return true;
else if(distance ==a.distance){
if(cost<a.cost)
return true;
}
return false;
}
};
struct road{
int length;
int cost;
road(int l,int c):length(l),cost(c){}
};
int main(){
int N,M,S,D;
cin>>N>>M>>S>>D;
vector<vector<road> > roads(N,vector<road>(N,road(-1,-1)));
int a,b,c,d;
while(M--){
cin>>a>>b>>c>>d;
roads[a][b].length=c;
roads[b][a].length=c;
roads[a][b].cost=d;
roads[b][a].cost=d;
}
vector<city> UD;
for(int i=0;i<N;i++){
if(i!=S)
UD.push_back(city(i,MAX,MAX));
}
city cur=city(S,0,0);
vector<int> preCity(N);//前驱数组,用于输出路径
preCity[S]=-1;
while(cur.num!=D){
for(auto it=UD.begin();it!=UD.end();++it){
if(roads[cur.num][(*it).num].length>0){//有路
int tmpL=cur.distance+roads[cur.num][(*it).num].length;
if(tmpL<(*it).distance){
(*it).distance=tmpL;
(*it).cost=cur.cost+roads[cur.num][(*it).num].cost;
preCity[(*it).num]=cur.num;
}else if(tmpL==(*it).distance){
int tmpC=cur.cost+roads[cur.num][(*it).num].cost;
if(tmpC<(*it).cost){
(*it).cost=tmpC;
preCity[(*it).num]=cur.num;
}
}
}
}
sort(UD.begin(),UD.end());
cur=UD[0];
UD.erase(UD.begin());
}
vector<int> result;
int k=D;
while(k!=S){//逆序压入路径,不包含起点
result.push_back(k);
k=preCity[k];
}
cout<<S;
for(auto it=result.rbegin();it!=result.rend();++it)
cout<<" "<<*it;
cout<<" "<<cur.distance<<" "<<cur.cost<<endl;
return 0;
}