UVa 10537 - The Toll! Revisited

提示: 1. 用最短路算法都可以解决 , 但要想好状态是什么意思(d[i] 代表什么东西)

    2. 开头结尾要注意 , 本题是开头不收税 , 结尾收税

    3. 'A'的优先级比'a'要靠前 , Debug中有一组数据可以看看

    4. 题中自己yy了一个函数来求收税以前的值 , 貌似是对的 , 但不会严格证明 , 有兴趣的拿不等式证证 , 记得告诉我


注意: 为了之后好推路径 ,id 函数要注意以字母序


//
//  main.cpp
//  UVa10537
//
//  Created by Fuxey on 15/10/7.
//  Copyright © 2015年 corn.crimsonresearch. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <algorithm>

using namespace std;
const long long n = 52;

inline long long id(char a) { return a>='a' && a<='z'? (long long)26+(long long)(a-'a') : (long long)(a-'A'); }
inline long long turn(long long a)
{
    long long b = a*20/19;
    return a+(b+19)/20;
}
long long d[n] , book[n] , vis[n];
long long g[n][n];
const long long INF = 1LL<<61;

int main(int argc, const char * argv[]) {
    
    long long m , Case=0;
    while(cin>>m && m!=-1)
    {
        memset(g, 0, sizeof(g));
        while(m--)
        {
            char a , b;
            cin>>a>>b;
            g[id(a)][id(b)] = 1;
            g[id(b)][id(a)] = 1;
        }
        
        fill(d, d+n, INF);
        char ss , ee;
        long long much , s , e;
        cin>>much>>ss>>ee;
        s = id(ss); e = id(ee);
        
        d[e] = much;
        if(e<=25) d[e] = turn(d[e]); else d[e]++;
        
        memset(book, 0, sizeof(book));
        memset(vis, 0, sizeof(vis)); vis[e] = 1;
        for(long long i=0;i<n;i++)
        {
            long long Min=INF , x = 0;
            for(long long j=0;j<n;j++) if(!book[j] && Min>d[j]) Min = d[j] , x = j;
            book[x] = 1;
            for(long long j=0;j<n;j++) if(g[x][j])
            {d[j] = (vis[j]? min(d[j], (j>=26?d[x]+1:turn(d[x]))) : (j>=26?d[x]+1:turn(d[x]))); vis[j] = 1;}
        }
        
        cout<<"Case "<<++Case<<":\n";
        cout<<(s<=25?d[s]-(d[s]+19)/20:d[s]-1)<<endl;
        while(s!=e)
        {
            char ss = s<26?'A'+s:'a'+s-26;
            cout<<ss<<'-';
            long long sup = s>=26?d[s]-1:d[s]-(d[s]+19)/20;
            for(long long i=0;i<n;i++) if(d[i]==sup) { s = i; break; }
        }
        char sss = s<26?'A'+s:'a'+s-26;
        cout<<sss<<endl;
    }
    
    
    return 0;
}

文章有任何纰漏 , 或者有任何问题 , 请告知博主: QQ:812483101 

你可能感兴趣的:(图论,最短路,uva)