hdoj 3499 Flight(最短路)
来源:互联网 发布:网络推广的工作 编辑:程序博客网 时间:2024/06/11 07:40
链接:点击打开链接
题意:可以让一条边的花费减半,求起到到终点的最短路。
思路:可以从起点和终点分别求一次最短路,dis1数组记录从起点到各点的最短路,
dis2数组记录从终点到各点的最短路。然后再枚举每一条边,判断这条边花费减半后加
上起点到这条边左端点的最短路再加上终点到这条边的右端点后的总花费是否更少,
ans = min(ans, dis1[u]+dis2[v]+w/2);
代码:
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<map>#include<queue>#include<set>using namespace std;typedef long long ll;const int maxn = 1e6+5;const ll INF = 1e18;int k, n, id, m, head1[maxn], head2[maxn];ll dis1[maxn], dis2[maxn];bool book[maxn];struct node{ int u, v, w, next;}edge1[maxn], edge2[maxn];void addEdge(struct node *edge, int *head, int u, int v, int w){ edge[k].u = u; edge[k].v = v; edge[k].w = w; edge[k].next = head[u]; head[u] = k;}void spfa(struct node *edge, int *head, int s, ll *dis){ memset(book, 0, sizeof(book)); for(int i = 0; i <= n; i++) dis[i] = INF; queue<int> q; dis[s] = 0; q.push(s); book[s] = 1; while(!q.empty()) { int u = q.front(); q.pop(); book[u] = 0; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; int w = edge[i].w; if(dis[u]+w < dis[v]) { dis[v] = dis[u]+w; if(!book[v]) { book[v] = 1; q.push(v); } } } }}void solve(int s, int e){ if(dis1[e] == INF) puts("-1"); else { ll ans = dis1[e]; for(int i = 0; i < k; i++) { int u = edge1[i].u; int v = edge1[i].v; ll w = edge1[i].w; ans = min(ans, dis1[u]+dis2[v]+w/2); } printf("%lld\n", ans); }}int main(void){ while(cin >> n >> m) { k = 0; id = 1; memset(head1, -1, sizeof(head1)); memset(head2, -1, sizeof(head2)); map<string, int> ID; set<string> s; for(int i = 0; i < m; i++) { string x, y; int d; cin >> x >> y >> d; int X, Y; if(s.count(x)) X = ID[x]; else ID[x] = id++, s.insert(x), X = id-1; if(s.count(y)) Y = ID[y]; else ID[y] = id++, s.insert(y), Y = id-1;// cout << X << ' ' << Y << endl; addEdge(edge1, head1, X, Y, d); addEdge(edge2, head2, Y, X, d); k++; } string S, E; cin >> S >> E; if(!s.count(S) || !s.count(E)) puts("-1"); else { int X = ID[S], Y = ID[E]; spfa(edge1, head1, X, dis1); spfa(edge2, head2, Y, dis2); solve(X, Y); } } return 0;}
Flight
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2724 Accepted Submission(s): 559
Problem Description
Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There's a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?
Input
There are no more than 10 test cases. Subsequent test cases are separated by a blank line.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000
0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000
0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
Output
One line for each test case the least money Shua Shua have to pay. If it's impossible for him to finish the trip, just output -1.
Sample Input
4 4Harbin Beijing 500Harbin Shanghai 1000Beijing Chengdu 600Shanghai Chengdu 400Harbin Chengdu4 0Harbin Chengdu
Sample Output
800-1HintIn the first sample, Shua Shua should use the card on the flight from Beijing to Chengdu, making the route Harbin->Beijing->Chengdu have the least total cost 800. In the second sample, there's no way for him to get to Chengdu from Harbin, so -1 is needed.
Author
Edelweiss
Source
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT
0 0
- hdoj 3499 Flight(最短路)
- HDOJ 最短路
- HDOJ 2544 最短路
- HDOJ 2544 最短路
- HDOJ-2544最短路
- HDOJ 2544 最短路
- HDOJ 2544 最短路
- hdoj 2544 最短路
- hdoj 2544 最短路
- HDOJ-2544最短路
- hdoj 2544 最短路
- HDOJ--2544--最短路
- hdoj 2544 最短路
- hdoj 2544 最短路
- HDOJ 2544 最短路
- hdoj 2544 最短路
- HDOJ 最短路
- hdoj 2544 最短路
- 记录red hat linux&nbsp…
- 初识Linux之常用命令二
- linux的运行级别设置
- 掌上党校
- 勿忘音乐播放器
- hdoj 3499 Flight(最短路)
- 卖书的
- android测量控件在屏幕上的坐标
- 将当前可视化组件进行截图保存为图…
- C++ vector 学习
- 如何将Android应用程序窗口的背景…
- 冒泡排序的C语言实现
- 在TextView中添加图片的三种方式
- android中自定义span对象的动作