UVA10537 最短路

从终点到起点倒推一遍最短路,此外我用了一个特殊的cost函数来根据当前载货量来动态的计算每条边的权重。
另一个卡点是在最短路的前提下,按字典序输出路径。 可以在“松弛”操作上做点小文章,即当 “新dist与旧dist值相等” 并且 “新的当前节点编号<旧的当前节点编号” 时更新存储着父节点的fa数组。

#include <stack>
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <cctype>

using namespace std;
typedef long long ll;
const ll BIG=1;
const ll SMALL=0;
const ll inf=0x3f3f3f3f3f3f3f;
const ll maxn=125+10;
ll getcost(ll d,ll t){
	if(t==BIG){
		ll x=d/19;
		if(d%19>0) x++;
		return x;
	}
	else return 1;
}
struct qnode{
    ll v;
	ll c;
    qnode(ll _v=0,ll _c=0):v(_v),c(_c){}
    bool operator <(const qnode &r)const{
        return c>r.c;
    }
};
struct Edge{
    ll v,t;
    Edge(ll _v=0,ll _cost=0):v(_v),t(_cost){}
};
vector<Edge> e[maxn];
bool vis[maxn];
ll dist[maxn];
char fa[maxn];
void dijkstra(ll n,ll start,ll initvalue){
    memset(vis,false,sizeof(vis));
    //memset(dist,inf,sizeof(dist));
	for(ll i=0;i<maxn;i++) dist[i]=inf;
    priority_queue<qnode> q;
    dist[start]=initvalue;
    q.push(qnode(start,0));
    qnode tmp;
    while(!q.empty()){
        tmp=q.top();
        q.pop();
        ll u=tmp.v;
        if(vis[u]) continue;
        vis[u]=true;
        for(ll i=0;i<e[u].size();i++){
            ll v=e[u][i].v;
            ll cost=getcost(dist[u],e[u][i].t);
            if(!vis[v]){
				if((dist[v]>dist[u]+cost)||((dist[v]==dist[u]+cost)&&fa[v]>u)){
					fa[v]=u;
	                dist[v]=dist[u]+cost;
	                q.push(qnode(v,dist[v]));
				}
            }
        }
    }
}
int main(){
	ll t=0;
	ll m,good;
	char u,v,source,target;
	while(~scanf("%lld",&m)&&m!=-1){
		for(ll i=0;i<maxn;i++) e[i].clear();
		for(ll i=0;i<m;i++){
			scanf("\n%c %c",&u,&v);
			if(u==v) continue;
			if(isupper(v)) e[v].push_back(Edge(u,BIG));
			else e[v].push_back(Edge(u,SMALL));
			if(isupper(u)) e[u].push_back(Edge(v,BIG));
			else e[u].push_back(Edge(v,SMALL));
		}
		scanf("%lld %c %c",&good,&target,&source);
		dijkstra(128,source,good);
		printf("Case %lld:\n",++t);
		printf("%lld\n",dist[target]);
		char x=target;
		while(x!=source){
			printf("%c-",x);
			x=fa[x];
		}
		printf("%c\n",source);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值