<span style="font-size:48px;"><strong>1020. Tree Traversals (25)</strong></span>
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
# include <cstdio>
# include <cstring>
# include <climits>
# include <queue>
# include <algorithm>
using namespace std;
const int _size = 500;
int Dist[_size][_size];
typedef pair<int,int> dist;
int dis[5000];
int pre[5000];
int main()
{
int n,m,s,e;
scanf("%d%d%d%d",&n,&m,&e,&s);
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
Dist[i][j] = INT_MAX/4;
memset(pre,-1,sizeof(pre));
while (m--)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
Dist[a][b] = Dist[b][a] = c*10000 + d;
}
priority_queue<dist,vector<dist>,greater<dist> > h;
for (int i=0;i<n;i++)
dis[i] = INT_MAX/4;
dis[s] = 0;
int f = -1,c = s;
h.push(dist(dis[s],s));
while (!h.empty())
{
dist loca = h.top();h.pop();
int city = loca.second;
if (dis[city] < loca.first) continue;
for (int i=0;i<n;i++)
{
if (dis[city] + Dist[city][i] < dis[i])
{
dis[i] = dis[city] + Dist[city][i];pre[i] = city;
h.push(dist(dis[i],i));
}
}
}
f = e;
while (f!=-1)
{
printf("%d ",f);
f = pre[f];
}
printf("%d %d\n",dis[e]/10000,dis[e]%10000);
return 0;
}
然而这份代码在牛客网上貌似是无法通过的,所以勤奋的我又用写一个wight类的思想,再写了一份代码
# include <cstdio>
# include <climits>
# include <iostream>
# include <algorithm>
# include <queue>
# include <stack>
using namespace std;
struct weight
{
int lenth,cost;
weight(int _lenth = (INT_MAX>>1),int _cost = 0):lenth(_lenth),cost(_cost){}
weight operator + (weight& oper)
{
return weight(lenth + oper.lenth,cost + oper.cost);
}
bool operator < (const weight& oper) const
{
if (lenth!=oper.lenth)
return lenth < oper.lenth;
return cost < oper.cost;
}
void Read()
{
cin >> lenth >> cost;
}
};
struct edge
{
int to;
weight value;
edge *next;
edge(int _to,weight& _w):to(_to),value(_w),next(NULL){}
};
struct vertex
{
weight w_sum;
int prev;
edge *next,*last;
vertex():prev(-1),next(NULL),last(NULL){}
void Attach(int _to,weight& _value)
{
if (next == NULL)
next = last = new edge(_to,_value);
else
last = last->next = new edge(_to,_value);
}
};
vertex city[501];
int main()
{
int n,m,s,e;
cin >> n >> m >> s >> e;
while (m--)
{
int a,b;
weight temp;
cin >> a >> b;
temp.Read();
city[a].Attach(b,temp);
city[b].Attach(a,temp);
}
typedef pair<weight,int> p;
priority_queue<p,vector<p>,greater<p> > pq;
city[s].w_sum.lenth = 0;
pq.push(p(city[s].w_sum,s));
while (!pq.empty())
{
p loca = pq.top();pq.pop();
int c = loca.second;
weight w = loca.first;
if (city[c].w_sum < w)
continue;
edge *next = city[c].next;
while (next)
{
if (w + next->value < city[next->to].w_sum)
{
city[next->to].w_sum= w + next->value;
pq.push(p(city[next->to].w_sum,next->to));
city[next->to].prev = c;
}
next = next->next;
}
}
stack<int> stac;
int temp = e;
stac.push(e);
while (city[temp].prev!=-1)
{
stac.push(city[temp].prev);
temp = city[temp].prev;
}
while (!stac.empty())
{
cout << stac.top() << ' ';
stac.pop();
}
cout << city[e].w_sum.lenth << ' ' << city[e].w_sum.cost << endl;
return 0;
}