HDU

来源:互联网 发布:淘宝上卖精密管犯法吗? 编辑:程序博客网 时间:2024/05/18 02:13

Flight

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2985    Accepted Submission(s): 625


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 4Harbin Beijing 500Harbin Shanghai 1000Beijing Chengdu 600Shanghai Chengdu 400Harbin Chengdu4 0Harbin 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.
 

这题写了好久,刚开始想就是直接边跑边更新,就是直接算起点到那一点的路径减去路径中最大的一个的一半,求最小,后来发现这样考虑有些情况是会漏掉的,比如说起点为A,终点为B,有一点C,AC 有两个值,其中一个为100,最大边为25,另一个为150,最大边为100,此时应该选第二个,但是如果有一条边CB长度为100,则又应该选择第一个。这就有问题了,后来又想了种办法,就是先把A到所有点的最短距离求出,再求减去最长边的最小距离,就是每次要三选一,cost[A]+max(dist[A][B]/2,mx[A]),cost_1[A]+dist[A][B]/2,cost[B] - mx[B]/2,写起来十分的复杂,然后变来变去的,有点问题,找了题解,看了看别人的思路,有什么分层法,dp什么的,不过还是建反向图最为简单易懂。

求出A到其余各点的最短路,建反向图求B到其他各点的最短路,遍历除终点外其他各点,令该点C1出发到下一点C2为打折路线,起点A到终点B 的距离为 cost_A[C1]+cost_B[C2]+dist[C1][C2]/2遍历所有点后,最小值即为答案。


#include<iostream>#include<stdio.h>#include<string.h>#include<map>#include<queue>using namespace std;#define ll long long#define INF 10000000000 + 10000const ll size = 101000;ll n,m,d,cnt,cost[2][size],st,ed,head[2][size];struct node{    ll to,next,cost;}e[2][size*5];map<string,ll>mp;string s1,s2;void add_e(int x,ll from,ll to,ll cost,int i){    e[x][i].cost = cost;    if(head[x][from]==-1)    {        head[x][from] = i;        e[x][i].to = to;    }    else    {        ll he = head[x][from];        ll next = e[x][he].next;        e[x][he].next = i;        e[x][i].next = next;        e[x][i].to = to;    }}void spfa(int x,ll st){    queue<ll>q;    q.push(st);    bool in_que[size];    memset(in_que,0,sizeof in_que);    cost[x][st] = 0;    while(!q.empty())    {        st = q.front();        q.pop();        in_que[st] = false;        ll pos = head[x][st];        while(pos!=-1)        {            if(cost[x][e[x][pos].to]>cost[x][st]+e[x][pos].cost)            {                cost[x][e[x][pos].to] = cost[x][st] + e[x][pos].cost;                if(in_que[e[x][pos].to]==false)                {                    in_que[e[x][pos].to] = true;                    q.push(e[x][pos].to);                }            }            pos = e[x][pos].next;        }    }}int main(){    while(~scanf("%lld%lld",&n,&m))    {        for(int i=0;i<=n;i++)            cost[0][i] = cost[1][i] = INF;        mp.clear();        memset(e,-1,sizeof e);        memset(head,-1,sizeof head);        cnt = 1;        for(int i=0;i<m;i++)        {            cin>>s1>>s2>>d;            if(!mp[s1]) mp[s1] = cnt++;            if(!mp[s2]) mp[s2] = cnt++;            add_e(0, mp[s1], mp[s2], d, i);            add_e(1, mp[s2], mp[s1], d, i);        }        cin>>s1>>s2;        if(!mp[s1]) mp[s1] = cnt++;        if(!mp[s2]) mp[s2] = cnt++;        st = mp[s1],ed = mp[s2];        spfa(0,st);        spfa(1,ed);        if(cost[0][ed]>=INF)            printf("-1\n");        else        {            ll mi = INF;            for(int i=1;i<=n;i++)            {                if(i==ed)                    continue;                ll cost_a = cost[0][i];                ll pos = head[0][i];                while(pos!=-1)                {                    ll cost_b = cost[1][e[0][pos].to];                    mi = min(mi,cost_a+cost_b+e[0][pos].cost/2);                    pos = e[0][pos].next;                }            }            printf("%lld\n",mi);        }    }    return 0;}



原创粉丝点击