Uva-10537-The Toll! Revisited

150 篇文章 0 订阅

用最短路径解决,注意要从终点向起点进行找最短路径,代价的时候有一定的技巧(最初求代价和最后输出路径时候求代价是不同的),最后注意城市的起点与终点的问题,我在这里WA了N次,哎~

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int maxn=1001;
const long long  inf=1LL<<45;
struct Edge
{
    int from;
    int to;
};
struct HeapNode
{
    long long d;
    int u;
    bool operator <(const HeapNode& a)const
    {
	return d>a.d;
    }
};
struct Dijkstra{
    int n,m;
    vector<Edge> edges;
    vector<long long> G[maxn];
    vector<int> way;
    bool vis[maxn];
    int pre[maxn];
    long long dist[maxn];
    bool has[maxn][maxn];
    void Init(int n)
    {
	this->n=n;
	for(int i=0;i<=n;i++)
	    G[i].clear();
	edges.clear();
	way.clear();
	memset(has,0,sizeof(has));
    }
    int GetNum(char ch)
    {
	if(ch<='Z')
	    return ch-'A';
	else
	    return ch-'a'+26;
    }
    long long CalCost(int from,int to)
    {
	if(from<26)
	    return (dist[from]+18LL)/19LL;
	return 1;
    }
    long long CalCostA(int from,int to)
    {
	if(to<26)
	    return (dist[from]+19LL)/20LL;
	return 1;
    }
    void Addedge(int from ,int to)
    {
	edges.push_back((Edge){from,to});
	m=edges.size();
	G[from].push_back(m-1);
	has[from][to]=1;
    }
    void GetPaths(int s,int e)
    {
	if(s==e)
	    return;
	for(int i=0;i<=52;i++)
	{
	    if(has[s][i]&&dist[s]-CalCostA(s,i)==dist[i])
	    {
		way.push_back(i);
		GetPaths(i,e);
		return;
	    }
	}
    }
    long long dijkstra(int s,int to,long long dis)
    {
	priority_queue<HeapNode> Q;
	for(int i=0;i<=n;i++)
	    dist[i]=inf;
	dist[s]=dis;
	memset(vis,0,sizeof(vis));
	Q.push((HeapNode){dis,s});
	while(!Q.empty())
	{
	     HeapNode x=Q.top();
	     Q.pop();
	     int u=x.u;
	     if(vis[u])
		 continue;
	     vis[u]=1;
	     for(int i=0;i<G[u].size();i++)
	     {
		 Edge e=edges[G[u][i]];
		 long long cost=CalCost(u,e.to);
		 if(dist[e.to]>dist[u]+cost)
		 {
		     dist[e.to]=dist[u]+cost;
		     pre[e.to]=u;
		     Q.push((HeapNode){dist[e.to],e.to});
		 }
	     }
	}
	return dist[to];
    }
}dis;
int main()
{
    int n,cas=1;
    while(scanf("%d",&n)&&n!=-1)
    {
	dis.Init(maxn-1);
	char ita[3],itb[3];
	for(int i=0;i<n;i++)
	{
	    scanf("%s%s",ita,itb);
	    int a=dis.GetNum(ita[0]);
	    int b=dis.GetNum(itb[0]);
	    dis.Addedge(a,b);
	    dis.Addedge(b,a);
	}
	long long itc;
	scanf("%lld%s%s",&itc,ita,itb);
	int a=dis.GetNum(ita[0]);
	int b=dis.GetNum(itb[0]);
	long long ans=dis.dijkstra(b,a,itc);
	printf("Case %d:\n",cas++);
	printf("%lld\n",ans);
	dis.GetPaths(a,b);
	if(a<26)
	    printf("%c",a+'A');
	else
	    printf("%c",a-26+'a');
	for(int i=0;i<dis.way.size();i++)
	{
	    if(dis.way[i]<26)
		printf("-%c",dis.way[i]+'A');
	    else
		printf("-%c",dis.way[i]-26+'a');
	}
	printf("\n");

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值