HDU 3499 Flight spfa+dp

Flight

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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.

/*
这题刚开始我以为只要先找到最短路径,然后再把机票最贵的折半就行了,
后来发现,不一定。。。。这样最便宜(。﹏。)
上网搜了一下,有的是spfa+dp有的分层图,我决定把两种都写一遍(?•??•?)??
*/

/* spfa+dp
 dis[0][i]表示到i点折半的花费 dis[1][i]表示到i点没用过折半的花费
这题还要注意的是花费总额已经超出int范围,要用long long
然而我inf最大值没有改成LLONG_MAX错了好几次
*/

#include <iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
long long dis[100005][2];
bool vis[100005];
struct node
{
    int num,d;
    node(int a,int b){num=a; d=b;}
};
vector<node> s[100005];
int n,m;
const long long inf=LLONG_MAX;
map<string,int> mp;
void spfa(int k)
{
    for(int i=1;i<=n;i++) dis[i][1]=dis[i][0]=inf,vis[i]=0;
    queue<int> Q;
    Q.push(k);
    vis[k]=1;
    dis[k][1]=dis[k][0]=0;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        vis[u]=0;
        for(int i=0;i<s[u].size();i++)
        {
            bool flag=0;
            if (dis[s[u][i].num][1]>dis[u][1]+s[u][i].d)
            {
                flag=1;
                dis[s[u][i].num][1]=dis[u][1]+s[u][i].d;
            }
            if (dis[s[u][i].num][0]>dis[u][0]+s[u][i].d || dis[s[u][i].num][0]>dis[u][1]+s[u][i].d/2)
            {
                dis[s[u][i].num][0]=min(dis[u][0]+s[u][i].d,dis[u][1]+s[u][i].d/2);
                flag=1;
            }
            if(flag && !vis[s[u][i].num])
            {
                vis[s[u][i].num]=1;
                Q.push(s[u][i].num);
            }
        }
    }
    return;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int l=0,d;
        char ch2[15],ch1[15];
        for(int i=1;i<=n;i++) s[i].clear();
        mp.clear();
        for(int i=1;i<=m;i++)
        {
            scanf("%s %s %d",&ch1,&ch2,&d);
            if (!mp[ch1]) mp[ch1]=++l;
            if (!mp[ch2]) mp[ch2]=++l;
            s[mp[ch1]].push_back( node(mp[ch2],d) );
        }
        scanf("%s %s",&ch1,&ch2);
        if (!mp[ch1]) mp[ch1]=++l;//可能m=0,所以有可能开始和结束城市都没有编号
        if (!mp[ch2]) mp[ch2]=++l;
        int scity=mp[ch1];  //开始的城市编号
        int ecity=mp[ch2]; //相要到达的城市编号
        spfa(scity);
        if (dis[ecity][0]==inf) printf("-1\n");
           else   printf("%lld\n",dis[ecity][0]);
    }
    return 0;
}

最后还是拉了一段代码,(⊙﹏⊙)b

/*

转自:http://yomean.blog.163.com/blog/static/189420225201110282390985/

一看就想到了分层图,不过如果用分层图,有点杀鸡用牛刀的感觉,因为只有两层。但我还是写了,最后AC了。不过网上很多人都是用建反两向边求解。
而对于分层图求最短路径问题,我们要注意的是,层与层之间的连线都是单向的,而且是从下一层指向上一层,而我们求最短路径的时候,起点总是在下一层,而终点总是在上一层,所以我们可以将经过层与层之间的特殊边的数目控制在n - 1(n是层数)。

*/
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<map>
#include<vector>
#define N 100005
#define inf (_I64_MAX)/2
using namespace std;
int n,m;
int head[2*N],vis[2*N];
int now,index,k;
__int64 dis[2*N];
char name[N][10];
map<string,int>M;
struct node{
    int v,w,next;
}edge[15*N];
void addedge(int u,int v,int w)
{
    edge[index].v=v;
    edge[index].w=w;
    edge[index].next=head[u];
    head[u]=index++;
}
struct cmp{
    bool operator()(int a,int b){
         return dis[a]>dis[b];
    }
};
priority_queue<int,vector<int>,cmp>Q;
void init()
{
    while(!Q.empty()) Q.pop();
    M.erase(M.begin(),M.end());
    for(int i=0;i<2*n;i++){
        vis[i]=false;
        head[i]=-1;
    }
    now=1;
    index=0;
}
void dij(int s,int e)
{
    for(int i=0;i<=2*n;i++){
       dis[i]=inf;vis[i]=false;
    }
    dis[s]=0;
    vis[s]=true;
    Q.push(s);
    while(!Q.empty()){
        int u=Q.top();
        Q.pop();
        if(u==e){
            printf("%I64d\n",dis[u]);
            return;
        }
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            int w=edge[i].w;
            if(!vis[v] && dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                Q.push(v);
            }
        }
    }
}
int main(void)
{
    string a,b;
    int x,y,c;
    while(cin>>n>>m)
    {
        init();
        for(int i=0;i<m;i++){
            cin>>a>>b>>c;
            if(M.find(a)==M.end()) M[a]=now++;
            if(M.find(b)==M.end()) M[b]=now++;
            addedge(M[a],M[b],c);
            addedge(M[a]+n,M[b]+n,c);
            addedge(M[a]+n,M[b],c/2);
        }
        cin>>a>>b;
        __int64 ans=inf;
        if(M.find(a)==M.end() || M.find(b)==M.end()){
            puts("-1");continue;
        }
        else dij(M[a]+n,M[b]);
    }
    return 0;
}
时间: 2024-10-17 22:14:13

HDU 3499 Flight spfa+dp的相关文章

hdu 3499 Flight dijkstra 变形

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 hel

HDU - 3499 Flight 双向SPFA+枚举中间边

Flight 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

HDU 4284 状压dp+spfa堆优化

题意: 给定n个点 m条无向边 d元. 下面m行表示每条边 u<=>v 以及花费 w 下面top 下面top行 num c d 表示点标为num的城市 工资为c 健康证价格为d 目标是经过给定的top个城市,当到达该城市时,必须马上购买该城市的健康证并打工赚钱(每个城市只打工1次) 问从1城市出发,最后回到1城市,能否收集到所有的健康证 思路: 由于top很小,所以状压dp dp[i][tmp]表示当前处于i点 经过城市的状态为tmp时 身上最多的钱. 首先对dis数组floyd 跑出最短路,

HDU 4892 状压dp

[BestCoder Round #5]冠军的奖励是小米3手机一部 恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部) <BestCoder用户手册>下载 Defence of the Trees Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 224    Accepted Submiss

HDU 4960 (水dp)

Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. This morning, his children played with plasticene. They broke the plasticene into N pieces, and put them in a line. Each piece has a volume Vi. Since Xi

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

hdu 2296 aC自动机+dp(得到价值最大的字符串)

Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3180    Accepted Submission(s): 1033 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a rom

hdu 2457 AC自动机+dp

DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2004    Accepted Submission(s): 1085 Problem Description Biologists finally invent techniques of repairing DNA that contains segments c

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro