1. 程式人生 > >6290】 奢侈的旅行 (dijkstra+優化)

6290】 奢侈的旅行 (dijkstra+優化)

題目看起來有些有些複雜,但是理解起來並不是很困難,主要是在資料的處理上需要上心,因為資料wa了好多次   =  =。

解題思路:就算dijkstra模板,在這個基礎上增加一些判斷和處理,比如cost=log2((level+ai)/level)和cost<bi
,在基礎模板上增加判斷就可以

ac程式碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<cmath>
#define maxn 1000005
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
int n,m,cnt;
ll dis[maxn];
int mark[maxn];
int head[maxn];
struct node
{
	int u;
	int v;
	ll a;
	ll b;
	int next;
}nod[maxn];
struct Node
{
	int pos;
	ll  cost;
//	node(){}//沒有此建構函式不能寫  node t  這樣
//	node(int pos,int cost):pos(pos),cost(cost){}//可以寫node(pos,cost)這樣
	friend bool operator < (Node a,Node b)//運算子過載<   先把格式記住
	{
		return a.cost> b.cost;
	}
};

void add(int u,int v,ll a,ll b)//加邊
{
	nod[cnt].u=u;
	nod[cnt].v=v;
	nod[cnt].a=a;
	nod[cnt].b=b;
	nod[cnt].next=head[u];
	head[u]=cnt++;
}

void init()
{
	memset(head,-1,sizeof(head));
	memset(mark,0,sizeof(mark));
	memset(dis,inf,sizeof(dis));
	cnt=0;
}
void dijkstra()
{
	Node tmp,qq;
	tmp.pos=1;
	tmp.cost=1;
	priority_queue<Node> q;
	dis[1]=1;
	q.push(tmp);
	while(q.size())
	{
		tmp=q.top();
		q.pop();
		int u=tmp.pos;
		if(mark[u]) continue;
		mark[u]=1;
		for(int i=head[u];i!=-1;i=nod[i].next)
		{
			int v=nod[i].v;
			ll cos=nod[i].a;
			if((ll)cos/dis[u]<nod[i].b)
				continue;
			if(dis[v]>dis[u]+cos)
			{
				dis[v]=dis[u]+cos;
				qq.pos=v;
				qq.cost=dis[v];
				q.push(qq);
			}
		}		
	}
	if(dis[n]==inf)  printf("-1\n");
	else printf("%lld\n",(ll)(log2(dis[n]*1.0)));
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		init();
		scanf("%d%d",&n,&m);
		int u,v;
		ll a,b;
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%lld%lld",&u,&v,&a,&b);
			add(u,v,a,((ll)1<<b)-1);
		}
		dijkstra();
	}
	return 0;
}

需要注意的是,在進行對資料處理的時候,為了方便就將node結構體中存放的是2^cost-1  對之後的判斷和操作進行了簡化,但最後輸出的時候還是需要將其轉換回來,所以就會涉及到精度問題,題目要求了就是下取整,比較方便。

總結一句就是自己的dijkstra的鄰接表的使用還是超級水,靠著模板都會出一大堆錯誤。= = ,可怕可怕