2019 acm icpc西安邀請賽 M Travel

Travel 

  •  22.85%
  •  1000ms
  •  262144K

There are nn planets in the MOT galaxy, and each planet has a unique number from 1 \sim n1∼n. Each planet is connected to other planets through some transmission channels. There are mm transmission channels in the galaxy. Each transmission channel connects two different planets, and each transmission channel has a length.

The residents of the galaxy complete the interplanetary voyage by spaceship. Each spaceship has a level. The spacecraft can be upgraded several times. It can only be upgraded 11 level each time, and the cost is cc. Each upgrade will increase the transmission distance by dd and the number of transmissions channels by ee to the spacecraft. The spacecraft can only pass through channels that are shorter than or equal to its transmission distance. If the number of transmissions is exhausted, the spacecraft can no longer be used.

Alice initially has a 00-level spacecraft with transmission distance of 00 and transmission number of 00. Alice wants to know how much it costs at least, in order to transfer from planet 11 to planet nn.

Input

Each test file contains a single test case. In each test file:

The first line contains nn, mm, indicating the number of plants and the number of transmission channels

The second line contains cc, dd, ee, representing the cost, the increased transmission distance, and the increased number of transmissions channels of each upgrade, respectively.

Next mm lines, each line contains u,v,wu,v,w, meaning that there is a transmission channel between uu and vv with a length of ww.

(2 \le n\le 10^5, n - 1 \le m \le 10^5,1 \le u,v \le n ,1 \le c,d,e,w \le 10^5)(2≤n≤105,n−1≤m≤105,1≤u,v≤n,1≤c,d,e,w≤105)

(The graph has no self-loop , no repeated edges , and is connected)

Output

Output a line for the minimum cost. Output -1−1 if she can't reach.

樣例輸入複製

5 7
1 1 1
1 2 1
1 3 5
1 4 1
2 3 2
2 4 5
3 4 3
3 5 5

樣例輸出複製

5

二分升級次數,check能否走到 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
std::vector<pair<ll,ll> > G[maxn];
ll dis[maxn];int vis[maxn],cnt[maxn],n,m,c,d,e;
struct node
{
	ll dis,pos;
	bool operator<(const node& x)const
	{
		return dis>x.dis;
	}
};
priority_queue<node>q;
inline int dijkstra(int k)
{
	memset(vis,0,sizeof vis);
    memset(cnt,inf,sizeof cnt);
    memset(dis,inf,sizeof dis);
    dis[1]=cnt[1]=0;
    q.push((node){0,1});
    while(!q.empty())
    {
        node x=q.top();q.pop();
        ll u=x.pos;
        if(vis[u])continue;
        vis[u]=1;
        for(auto &it:G[u])
        {
        	if(dis[it.first]>=dis[u]+it.second&&it.second<=k*d)
        	{
        		dis[it.first]=dis[u]+it.second;
        		cnt[it.first]=min(cnt[it.first],cnt[u]+1);
        		q.push((node){dis[it.first],it.first});
        	}
        }
    }
    if(dis[n]<inf && cnt[n]<=k*e) return 1;
    return 0;
}
int main()
{
	scanf("%d%d",&n,&m);scanf("%d%d%d",&c,&d,&e);
	for(int i=0;i<m;i++)
	{
		int u,v,w;scanf("%d%d%d",&u,&v,&w);
		G[u].emplace_back(v,w);
		G[v].emplace_back(u,w);
	}
	int l=1,r=5000;
	while(l<r)
	{
		int m=(l+r)>>1;
		if(dijkstra(m)) r=m;
		else l=m+1;
	}
	if(dijkstra(r))printf("%lld\n",1LL*r*c);
	else printf("-1\n");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章