cf Volleyball

http://codeforces.com/problemset/problem/95/C

Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths.

Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially.

At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 ≤ x, y ≤ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 ≤ ti, ci ≤ 109), which describe the taxi driver that waits at the i-th junction — the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character.

Output

If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.

Examples

Input

Copy

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

Output

Copy

9

Note

An optimal way — ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.

 

先对每个点dijkstra(i)一遍,记录所有在t(i)之内能到达的点,建一张新图,边就是每个i在t(i)内能到达的点,边权是c(i)。再跑一遍dijkstra()即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long

struct Edge{
	int from,to,dist;
};
struct HeapNode{
	int u;ll d;
	operator < (const HeapNode& x)const{
		return d>x.d;
	}
};
int n,m,s,t;
vector<Edge> edges,edges2;
vector<int> G[1000+10],G2[1000+10];
bool vis[1000+10];
ll d[1000+10];
int maxdis[1000+10],cost[1000+10];

void AddEdge(int f,int t,int d)
{
	edges.push_back((Edge){f,t,d});
	G[f].push_back(edges.size()-1);
}

void AddEdge2(int f,int t,int d)
{
	edges2.push_back((Edge){f,t,d});
	G2[f].push_back(edges2.size()-1);
}

bool last;
void dijkstra(int s,vector<Edge>& edges,vector<int>* G)
{
	priority_queue<HeapNode> Q;
	for(int i=1;i<=n;i++)d[i]=(1LL<<50);
	d[s]=0;
	memset(vis,0,sizeof(vis));
	Q.push((HeapNode){s,0});
	while(!Q.empty())
	{
		HeapNode x=Q.top();
		Q.pop();
		int u=x.u;   
		if(vis[u])continue;
		vis[u]=1; //if(last)cout<<u<<"d: "<<d[u]<<endl;
		for(int i=0;i<G[u].size();i++)
		{
			Edge& e=edges[G[u][i]];
			int v=e.to; 
			if(d[v]>d[u]+e.dist)
			{
				d[v]=d[u]+e.dist;
				if(last==0&&d[v]>maxdis[s])continue;
				Q.push((HeapNode){v,d[v]});
			}
		}
	}
}

int main()
{
//	freopen("input.in","r",stdin);
	while(cin>>n>>m)
	{
		cin>>s>>t;
		int a,b,c;
		while(m--)
		{
			cin>>a>>b>>c;
			AddEdge(a,b,c);
			AddEdge(b,a,c);
		}
		for(int i=1;i<=n;i++)
		{
			cin>>maxdis[i]>>cost[i];
			dijkstra(i,edges,G);
			for(int j=1;j<=n;j++)if(d[j]<=maxdis[i])AddEdge2(i,j,cost[i]);
		}
		last=1;
		dijkstra(s,edges2,G2);
		cout<<(d[t]<(1LL<<50)?d[t]:-1)<<endl;
	}
	return 0;
}

 

全部评论

相关推荐

05-11 11:48
河南大学 Java
程序员牛肉:我是26届的双非。目前有两段实习经历,大三上去的美团,现在来字节了,做的是国际电商的营销业务。希望我的经历对你有用。 1.好好做你的CSDN,最好是直接转微信公众号。因为这本质上是一个很好的展示自己技术热情的证据。我当时也是烂大街项目(网盘+鱼皮的一个项目)+零实习去面试美团,但是当时我的CSDN阅读量超百万,微信公众号阅读量40万。面试的时候面试官就告诉我说觉得我对技术挺有激情的。可以看看我主页的美团面试面经。 因此花点时间好好做这个知识分享,最好是单拉出来搞一个板块。各大公司都极其看中知识落地的能力。 可以看看我的简历对于博客的描述。这个帖子里面有:https://www.nowcoder.com/discuss/745348200596324352?sourceSSR=users 2.实习经历有一些东西删除了,目前看来你的产出其实很少。有些内容其实很扯淡,最好不要保留。有一些点你可能觉得很牛逼,但是面试官眼里是减分的。 你还能负责数据库表的设计?这个公司得垃圾成啥样子,才能让一个实习生介入数据库表的设计,不要写这种东西。 一个公司的财务审批系统应该是很稳定的吧?为什么你去了才有RBAC权限设计?那这个公司之前是怎么处理权限分离的?这些东西看着都有点扯淡了。 还有就是使用Redis实现轻量级的消息队列?那为什么这一块不使用专业的MQ呢?为什么要使用redis,这些一定要清楚, 就目前看来,其实你的这个实习技术还不错。不要太焦虑。就是有一些内容有点虚了。可以考虑从PR中再投一点产出
点赞 评论 收藏
分享
Ncsbbss:又想干活又想要工资,怎么什么好事都让你占了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务