hdoj 3499 Flight(最短路)

链接:点击打开链接

题意:可以让一条边的花费减半,求起到到终点的最短路。


思路:可以从起点和终点分别求一次最短路,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. 
 

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 -1
Hint
In 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
 


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值