1.题目描述:
Flight
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2942 Accepted Submission(s): 612
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 4 Harbin Beijing 500 Harbin Shanghai 1000 Beijing Chengdu 600 Shanghai Chengdu 400 Harbin Chengdu 4 0 Harbin 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
Recommend
2.题意概述:
有n个城市m条航班,小明有一次半价的机会,问你小明从s到t的最小花费
3.解题思路:
题型类似于HDU1217也是最短路的经典变形。
做法是正反做一遍最短路,最后再暴力枚举每条边减半。
注意的坑点之一是航班是单向的,建图时候一定要考虑这个;第二是数据会超ll,如果单单设0x7fffffff也不够,应该是1LL << 60比较稳,而且在最后枚举那个半价航班时候如果简单的ans = min(ans, dis1[u] + dis2[v] + cost_between_u_v / 2)时候如果dis1[u]和dis2[v]都是INF的话很容易爆long long,因此要预先判断。
4.AC代码:
01 - spfa+链式前向星(集训时候码的)
2308 | 32.2 |
#include <bits/stdc++.h>
#define INF 1LL << 60
#define maxn 1000010
#define N 100010
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7
using namespace std;
typedef long long ll;
int n, m, num;
ll dis1[N], dis2[N];
bool vis[N];
map<string, int> mp;
struct node
{
int to;
ll w;
node *next;
} p[maxn], *G1[N], *G2[N], *head;
inline int find(char *ch)
{
if (mp.count(string(ch)))
return mp[string(ch)];
return mp[string(ch)] = num++;
}
inline void init()
{
mp.clear();
memset(G1, NULL, sizeof(G1));
memset(G2, NULL, sizeof(G2));
head = p;
num = 0;
}
inline void addedge(int u, int v, ll w, node *G[])
{
head->to = v;
head->w = w;
head->next = G[u];
G[u] = head++;
}
inline void spfa(int sta, ll dis[], node *G[])
{
memset(vis, false, sizeof(vis));
fill(dis, dis + N, INF);
dis[sta] = 0;
vis[sta] = 1;
deque<int> q;
q.push_back(sta);
while (!q.empty())
{
int u = q.front();
q.pop_front();
vis[u] = false;
for (node *p = G[u]; p != NULL; p = p->next)
{
int to = p->to;
ll w = p->w;
if (dis[to] > dis[u] + w)
{
dis[to] = dis[u] + w;
if (!vis[to])
{
vis[to] = true;
if (!q.empty() && dis[to] <= dis[q.front()])
q.push_front(to);
else
q.push_back(to);
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
while (~scanf("%d%d", &n, &m))
{
init();
char s1[11], s2[11];
for (int i = 0; i < m; i++)
{
ll w;
scanf("%s%s%lld", s1, s2, &w);
int u = find(s1);
int v = find(s2);
addedge(u, v, w, G1);
addedge(v, u, w, G2);
}
scanf("%s%s", s1, s2);
int sta = mp[string(s1)];
int ed = mp[string(s2)];
spfa(sta, dis1, G1);
spfa(ed, dis2, G2);
//printf("%lld\n%lld\n", dis1[ed], dis2[sta]);
ll ans = INF;
for (int u = 0; u < n; u++)
{
for (node *p = G1[u]; p != NULL; p = p->next)
{
int v = p->to;
ll w = (p->w) / 2;
if (dis1[u] < INF && dis2[v] < INF)
ans = min(ans, dis1[u] + dis2[v] + w);
}
}
printf("%lld\n", ans == INF ? -1 : ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
02 - spfa+vector+deque优化
20288107 | 2017-03-31 21:31:08 | Accepted | 3499 | 2511MS | 50668K | 2319 B | G++ | DoveDragon |
#include <bits/stdc++.h>
#define INF 1LL << 60
#define maxn 100100
#define N 5111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
int v;
ll w;
node(int a, ll b) { v = a; w = b; }
};
map<string, int> mp;
vector<node> vt1[maxn], vt2[maxn];
ll dis1[maxn], dis2[maxn];
bool vis[maxn];
int cnt;
inline int find(char ch[])
{
if (mp.count(ch))
return mp[ch];
return mp[ch] = ++cnt;
}
inline void spfa(int sta, int n, ll dis[], vector<node> * vt)
{
memset(vis, 0, sizeof(vis));
fill(dis, dis + n + 1, INF);
deque<int> q;
vis[sta] = 1;
dis[sta] = 0;
q.push_back(sta);
while (!q.empty())
{
int u = q.front();
q.pop_front();
vis[u] = 0;
int sz = vt[u].size();
for (int i = 0; i < sz; i++)
{
int v = vt[u][i].v;
ll val = vt[u][i].w;
if (dis[v] > dis[u] + val)
{
dis[v] = dis[u] + val;
if (!vis[v])
{
vis[v] = 1;
if (!q.empty() && dis[v] <= dis[q.front()])
q.push_front(v);
else
q.push_back(v);
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, m;
char ch[11];
while (~scanf("%d%d", &n, &m))
{
cnt = 0;
mp.clear();
for (int i = 1; i <= n; i++)
{
vt1[i].clear();
vt2[i].clear();
}
for (int i = 0; i < m; i++)
{
int d;
scanf("%s", ch);
int u = find(ch);
scanf("%s", ch);
int v = find(ch);
scanf("%d", &d);
vt1[u].push_back(node(v, 1LL * d));
vt2[v].push_back(node(u, 1LL * d));
}
scanf("%s", ch);
int sta = find(ch);
scanf("%s", ch);
int ed = find(ch);
spfa(sta, cnt, dis1, vt1);
spfa(ed, cnt, dis2, vt2);
ll ans = INF;
for (int i = 1; i <= cnt; i++)
{
int sz = vt1[i].size();
for (int j = 0; j < sz; j++)
{
int to = vt1[i][j].v;
ll val = vt1[i][j].w / 2;
if (dis1[i] < INF && dis2[to] < INF)
ans = min(ans, dis1[i] + dis2[to] + val);
}
}
printf("%lld\n", ans == INF ? -1 : ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}