列表

详情


NC24760. [USACO 2010 Dec G]Big Macs Around the World

描述

Bessie is studying her favorite subject, Macroeconomics, in cowllege. For her final project, she will be presenting research on exchange rates between countries around the world.
In order to make her presentation more lively, she would like to show the relative prices of Big Macs around the world, despite their rather unsavory contents. To illustrate, suppose that Bessie would like to find smallest value of a Big Mac in a country given its value in some initial country and exchange rates from which other country's values can be calculated (as illustrated below):
* A Big Mac is worth 60 dollars in the United States
* The exchange rate from US dollars to Canadian dollars is 0.2 Canadian dollars per US dollar
* The exchange rate from US dollars to British Pounds is 5.00 British Pounds per US Dollar
* The exchange rate from British Pounds to Canadian dollars is 0.5 Canadian dollars per British Pound
* The exchange rate between Canadian dollars to US dollars is 5.00 US dollars per Canadian dollar
and Bessie would like to find the smallest possible value of a Big Mac in Canada that can be obtained by exchanging currencies. There are two ways:
* Going from US dollars directly to Canada dollars would yield a burger worth 60.00 US dollars * 0.2 Canadian dollars / US dollar = 12.00 Canadian dollars
* Going from US dollars to British Pounds to Canadian dollars would yield a burger worth 60.00 US* 5.00 GBP / 1 US * 0.5 C/ 1 GBP = 150.00 C (Canadian dollars).
Bessie would choose the former option, since she would much rather pay 12.00 Canadian dollars instead of 150.00 Canadian dollars for a Big Mac in Canada.
Bessie has N (1 <= N <= 2,000) countries conveniently labeled 1 to N that she would like to consider along with a list of M (1 <= M <= 25,000) exchange rates (0.1 < <= 10), each between countries i and j (1 <= i <= N; 1 <= j <= N).
Given the value V (1 <= V <= 1,000,000,000,000), which is not necessarily an integer, of the Big Mac in her starting country A (1 <= A <= N), help her find the smallest possible value of a Big Mac in country B (1 <= B <= N; B != A) after a series of currency conversions. If there is no minimum, output 0.
It is guaranteed that the answer is, if not 0, between 1 and 10^15. It is also guaranteed that, for any country's currency, it is possible to get to any other country's currency.

输入描述

* Line 1: Five space-separated numbers: N, M, V, A, B
* Lines 2..M+1: Three space-separated numbers: i, j,

输出描述

* Line 1: A single positive number, the price of the Big Mac, with absolute or relative error at most . If there is no minimum, output 0.

示例1

输入:

3 4 60 1 2 
1 2 0.2 
1 3 5 
3 2 0.5 
2 1 5

输出:

12.00

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 1061ms, 内存消耗: 1388K, 提交时间: 2019-10-13 21:29:38

#include <bits/stdc++.h>

using namespace std;

const int N = 200010;

int n, m, a, b, x, y;
long double v, w;

struct node
{
	int to, next;
	long double val;
} indexx[N];

int cnt, head[N], Hash[N];

void add(int x, int y, long double w)
{
	indexx[++cnt].to = y;
	indexx[cnt].next = head[x];
	indexx[cnt].val = w;
	head[x] = cnt;
}

long double l[N];
bool vis[N], flag;

void spfa()
{
	queue<int> q;
	for (int i = 1; i <= n; i++)
		l[i] = 1e16;
	l[a] = v;
	q.push(a);
	vis[a] = 1;
	while (q.size())
	{
		int u = q.front();
		q.pop();
		vis[u] = 0;
		for (int i = head[u]; i; i = indexx[i].next)
		{
			int to = indexx[i].to;
			if (l[to] > l[u] * indexx[i].val)
			{
				l[to] = l[u] * indexx[i].val;
				if (!vis[to])
				{
					q.push(to);
					vis[to] = 1;
					Hash[to]++;
					if (Hash[to] > n)
					{
						flag = 1;
						break;
					}
				}
			}
		}
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	scanf("%d%d%Lf%d%d", &n, &m, &v, &a, &b);
	for (int i = 1; i <= m; i++)
	{
		scanf("%d%d%Lf", &x, &y, &w);
		add(x, y, w);
	}
	spfa();
	printf("%Lf\n", flag == 1 ? 0 : l[b]);

	return 0;
}

C++(clang++ 11.0.1) 解法, 执行用时: 111ms, 内存消耗: 1628K, 提交时间: 2022-10-20 16:32:03

#include<bits/stdc++.h>
using namespace std;
int n,m,a,b;
#define  ll long long 
double  v;
const ll LNF=0x3f3f3f3f3f3f3f3f;
const int N=2e5+10,M=35000;
int h[N],e[N],ne[N],cnt[N];
double w[N],d[N];
bool vis[N];
int idx;
void add(int x,int y,double z)
{
	w[idx]=z;
	e[idx]=y;
	ne[idx]=h[x];
	h[x]=idx++;
}


bool dij()
{
    for(int i = 0;i<=n;++i) d[i] = 1e16;
	d[a]=v;
	queue<int>q;
	q.push(a);
	while(!q.empty())
	{
		int t=q.front();
		q.pop();
		vis[t]=0;
		for(int i=h[t];i!=-1;i=ne[i])
		{
			int j=e[i];
			if(d[j]>d[t]*w[i])
			{
				d[j]=d[t]*w[i];
				cnt[j]=cnt[t]+1;
				if(cnt[j]>=n)
				{
					return false;
				}
				if(!vis[j])
				{
					vis[j]=1;
					q.push(j);
				}
			}
		}
		
	}
	return true;
}
   
int main()
{
	cin>>n>>m>>v>>a>>b;
	memset(h,-1,sizeof(h));
	while(m--)
	{
		
		int i,j;
		double dis;
		cin>>i>>j>>dis;
		add(i,j,dis);
	}
	bool st=dij();
	if(!st)cout<<"0\n";
	else printf("%.2lf\n",d[b]);
} 

上一题