UVA - 11374 - Airport Express(堆优化Dijkstra)

Problem    UVA - 11374 - Airport Express

Time Limit: 1000 mSec

UVA - 11374 - Airport Express(堆优化Dijkstra)Problem Description

In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and the Commercial-Xpress. They travel at different speeds, take different routes and have different costs. Jason is going to the airport to meet his friend. He wants to take the Commercial-Xpress which is supposed to be faster, but he doesn’t have enough money. Luckily he has a ticket for the Commercial-Xpress which can take him one station forward. If he used the ticket wisely, he might end up saving a lot of time. However, choosing the best time to use the ticket is not easy for him. Jason now seeks your help. The routes of the two types of trains are given. Please write a program to find the best route to the destination. The program should also tell when the ticket should be used.

Input

The input consists of several test cases. Consecutive cases are separated by a blank line. The first line of each case contains 3 integers, namely N, S and E (2 ≤ N ≤ 500,1 ≤ S,E ≤ N), which represent the number of stations, the starting point and where the airport is located respectively. There is an integer M (1 ≤ M ≤ 1000) representing the number of connections between the stations of the Economy-Xpress. The next M lines give the information of the routes of the Economy-Xpress. Each consists of three integers X, Y and Z (X,Y ≤ N,1 ≤ Z ≤ 100). This means X and Y are connected and it takes Z minutes to travel between these two stations. The next line is another integer K (1 ≤ K ≤ 1000) representing the number of connections between the stations of the Commercial-Xpress. The next K lines contain the information of the CommercialXpress in the same format as that of the Economy-Xpress. All connections are bi-directional. You may assume that there is exactly one optimal route to the airport. There might be cases where you MUST use your ticket in order to reach the airport.

UVA - 11374 - Airport Express(堆优化Dijkstra)Output

For each case, you should first list the number of stations which Jason would visit in order. On the next line, output ‘Ticket Not Used’ if you decided NOT to use the ticket; otherwise, state the station where Jason should get on the train of Commercial-Xpress. Finally, print the total time for the journey on the last line. Consecutive sets of output must be separated by a blank line.

UVA - 11374 - Airport Express(堆优化Dijkstra)Sample Input

4 1 4 4 1 2 2 1 3 3 2 4 4 3 4 5 1 2 4 3

UVA - 11374 - Airport Express(堆优化Dijkstra)Sample Output

1 2 4

2

5

题解:考虑枚举用哪个商业票,为什么这么想呢,因为堆优化Dijkstra复杂度(n+m)logn,乘上个K,如果没有多组数据的话应该是能过的,其实可以做到更好,分别从起点和终点跑两遍最短路,这样对于枚举的用商业票的那一段来说就可以常数时间内算出总费用,因为最短路一定是w(u, v) + dist[u](起点到u最短路) + dist2[v](v到终点最短路),这样问题就在O(K)时间内解决了。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); struct Edge
{
int to, w, next;
} edge[maxm]; struct HeapNode
{
int dis, u;
bool operator<(const HeapNode &a) const
{
return dis > a.dis;
}
}; int tot, head[maxn];
int n, m, k;
int st, en; void init()
{
tot = ;
memset(head, -, sizeof(head));
} void AddEdge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
} int dist[maxn], dist2[maxn];
int pre[maxn], Next[maxn];
bool vis[maxn]; int Dijkstra()
{
memset(dist, INF, sizeof(dist));
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
priority_queue<HeapNode> que;
pre[st] = st;
dist[st] = ;
que.push((HeapNode){, st});
while (!que.empty())
{
HeapNode first = que.top();
que.pop();
int u = first.u;
if (vis[u])
continue;
vis[u] = true;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist[v] > dist[u] + edge[i].w)
{
pre[v] = u;
dist[v] = dist[u] + edge[i].w;
que.push((HeapNode){dist[v], v});
}
}
}
return dist[en];
} void Dijkstra2()
{
memset(dist2, INF, sizeof(dist2));
memset(vis, false, sizeof(vis));
memset(Next, -, sizeof(Next));
priority_queue<HeapNode> que;
dist2[en] = ;
Next[en] = en;
que.push((HeapNode){, en});
while (!que.empty())
{
HeapNode first = que.top();
que.pop();
int u = first.u;
if (vis[u])
continue;
vis[u] = true;
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (dist2[v] > dist2[u] + edge[i].w)
{
Next[v] = u;
dist2[v] = dist2[u] + edge[i].w;
que.push((HeapNode){dist2[v], v});
}
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
bool ok = false;
while (cin >> n >> st >> en)
{
init();
cin >> m;
int x, y, z;
for (int i = ; i < m; i++)
{
cin >> x >> y >> z;
AddEdge(x, y, z);
AddEdge(y, x, z);
}
int Min = Dijkstra();
//cout << "Min:" << Min << endl;
Dijkstra2();
cin >> k;
int ansu = -, ansv = -;
for(int i = ; i < k; i++)
{
cin >> x >> y >> z;
if(dist[x] + z + dist2[y] < Min)
{
Min = dist[x] + z + dist2[y];
ansu = x, ansv = y;
}
if(dist[y] + z + dist2[x] < Min)
{
Min = dist[y] + z + dist2[x];
ansu = y, ansv = x;
}
}
if(!ok)
ok = true;
else
cout << endl;
//cout << "Min:" << Min << endl;
if(ansu == - && ansv == -)
{
int tmp = st;
while(tmp != en)
{
cout << tmp << " ";
tmp = Next[tmp];
}
cout << en << endl;
cout << "Ticket Not Used" << endl;
cout << Min << endl;
}
else
{
int tmp = ansu;
stack<int> ans;
while(!ans.empty())
ans.pop();
while(tmp != st)
{
ans.push(tmp);
tmp = pre[tmp];
}
ans.push(st);
while(!ans.empty())
{
cout << ans.top() << " ";
ans.pop();
}
tmp = ansv;
while (tmp != en)
{
cout << tmp << " ";
tmp = Next[tmp];
}
cout << en << endl;
cout << ansu << endl;
cout << Min << endl;
}
}
return ;
}
上一篇:linux下安装mysql(rpm文件安装)


下一篇:Nginx Parsing HTTP Package、header/post/files/args Sourcecode Analysis