《P4568 [JLOI2011] 飞行路线》
题目描述
Alice 和 Bob 现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在 n 个城市设有业务,设这些城市分别标记为 0 到 n−1,一共有 m 种航线,每种航线连接两个城市,并且航线有一定的价格。
Alice 和 Bob 现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多 k 种航线上搭乘飞机。那么 Alice 和 Bob 这次出行最少花费多少?
输入格式
第一行三个整数 n,m,k,分别表示城市数,航线数和免费乘坐次数。
接下来一行两个整数 s,t,分别表示他们出行的起点城市编号和终点城市编号。
接下来 m 行,每行三个整数 a,b,c,表示存在一种航线,能从城市 a 到达城市 b,或从城市 b 到达城市 a,价格为 c。
输出格式
输出一行一个整数,为最少花费。
输入输出样例
输入 #1复制
5 6 1 0 4 0 1 5 1 2 5 2 3 5 3 4 5 2 3 3 0 2 100
输出 #1复制
8
说明/提示
数据规模与约定
对于 30% 的数据,2≤n≤50,1≤m≤300,k=0。
对于 50% 的数据,2≤n≤600,1≤m≤6×103,0≤k≤1。
对于 100% 的数据,2≤n≤104,1≤m≤5×104,0≤k≤10,0≤s,t,a,b<n,a=b,0≤c≤103。
另外存在一组 hack 数据。
代码实现:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<complex>
#include<map>
#include<queue>
#include<vector>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
struct Edge {
int destination, nextEdge, weight;
} edges[6000060];
struct Node {
int distance, identifier;
Node() {}
Node(int d, int id) : distance(d), identifier(id) {}
};
bool operator <(Node left, Node right) {
return left.distance > right.distance;
}
int nodeCount, edgeCount, upgradeCount;
int startNode, endNode;
int fromNode, toNode, edgeWeight;
int edgeIndex = 0;
int adjacencyHead[1000010];
int visited[1000010];
int shortestDistance[1000010];
void addEdge(int source, int target, int weight) {
edges[edgeIndex].destination = target;
edges[edgeIndex].nextEdge = adjacencyHead[source];
edges[edgeIndex].weight = weight;
adjacencyHead[source] = edgeIndex++;
}
void dijkstra(int sourceNode) {
memset(visited, 0, sizeof(visited));
memset(shortestDistance, INF, sizeof(shortestDistance));
priority_queue<Node> nodeQueue;
nodeQueue.push(Node(0, sourceNode));
shortestDistance[sourceNode] = 0;
while (!nodeQueue.empty()) {
Node current = nodeQueue.top();
nodeQueue.pop();
if (visited[current.identifier]) continue;
visited[current.identifier] = 1;
for (int i = adjacencyHead[current.identifier]; i != -1; i = edges[i].nextEdge) {
int neighbor = edges[i].destination;
if (shortestDistance[neighbor] > edges[i].weight + current.distance) {
shortestDistance[neighbor] = edges[i].weight + current.distance;
nodeQueue.push(Node(shortestDistance[neighbor], neighbor));
}
}
}
}
int main() {
memset(adjacencyHead, -1, sizeof(adjacencyHead));
scanf("%d%d%d", &nodeCount, &edgeCount, &upgradeCount);
scanf("%d%d", &startNode, &endNode);
for (int i = 1; i <= edgeCount; i++) {
scanf("%d%d%d", &fromNode, &toNode, &edgeWeight);
addEdge(fromNode, toNode, edgeWeight);
addEdge(toNode, fromNode, edgeWeight);
for (int j = 1; j <= upgradeCount; j++) {
addEdge(fromNode + (j * nodeCount), toNode + (j * nodeCount), edgeWeight);
addEdge(toNode + (j * nodeCount), fromNode + (j * nodeCount), edgeWeight);
addEdge(fromNode + ((j-1) * nodeCount), toNode + (j * nodeCount), 0);
addEdge(toNode + ((j-1) * nodeCount), fromNode + (j * nodeCount), 0);
}
}
for (int i = 1; i <= upgradeCount; i++)
addEdge(endNode + (i-1) * nodeCount, endNode + i * nodeCount, 0);
dijkstra(startNode);
printf("%d", shortestDistance[endNode + upgradeCount * nodeCount]);
return 0;
}