湖大训练赛4 Dwarf Tower

Dwarf Tower
Time Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:65536KB
Total submit users: 43, Accepted users: 26
Problem 12847 : No special judgement
Problem description

Little Vasya is playing a new game named “Dwarf Tower”. In this game there are n different items, which you can put on your dwarf character. Items are numbered from 1 to n. Vasya wants to get the item with number 1.
There are two ways to obtain an item:
• You can buy an item. The i-th item costs ci money.
• You can craft an item. This game supports only m types of crafting. To craft an item, you give two particular different items and get another one as a result.
Help Vasya to spend the least amount of money to get the item number 1.


Input

The first line of input contains two integers n and m (1 ≤ n ≤ 10 000; 0 ≤ m ≤ 100 000) — the number of different items and the number of crafting types.
The second line contains n integers ci — values of the items (0 ≤ ci ≤ 109).
The following m lines describe crafting types, each line contains three distinct integers ai, xi, yi — ai is the item that can be crafted from items xi and yi (1 ≤ ai; xi; yi ≤ n; ai ̸= xi; xi ̸= yi; yi ̸= ai).


Output

The output should contain a single integer — the least amount of money to spend.


Sample Input
5 3
5 0 1 2 5
5 2 3
4 2 3
1 4 5
3 1
2 2 1
1 2 3
Sample Output
2
2

刚开始比赛一直很顺,这题开始用暴力T了一发,准备用DFS继续撸一发,队友过来说是树形Dp,思想跟我差不多,好吧,然后嘴贱的顺便丢了一句:如果不是树形DP,接下来不会卡题4个小时吧?(当时对于这种神预测,我表示有种不祥的预感)。果然很快的月神就WR了。我然后修改了一下加入了回朔,不出所料T了。然后一直在想这种思路怎么了,哪里有bug。其实就是不能这样。然后一边开启群嘲模式。ORz...后来我又用了各种优美的姿势暴力破解,最小生成树,拓扑。。。结果都是T。无奈还被大一新生给压下来了。据说不用任何算法。呵呵。临近比赛结束实在忍不住好奇去求教了一下。看到她们居然是用按第一个数排序从小到大,然后按第一个数从大到小更新值。居然AC了。显然这是有问题,我随便出了一组数据给他们。然后无言以对。我也就无语了,这样的煞笔代码也能AC.我当然是针对这代码,没有指人,不要见怪。整场4个小时我们就一直纠结这题。姿势优美却A不了C。煞笔代码却能AC,这种情况碰到几次了,还有一次是在上一次的bestcoder。太伤了。深深感受的这游戏满满的恶意。后来,也就是晚上。月神提出了一个想法,构图的方法跟我类似,不过更新点的时候是用迪杰斯特拉的思想,我提出了几点建议,但是我还是T怕了,反正闲着也是蛋疼,就顺手敲了一下,提交。红色的AC!!!oh...my god!终于在月神的光环下,以正确的姿势AC了。人生无憾了。可以滚去睡觉了。代码就只贴AC的算了。。。心太伤。。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
struct node
{
	int x,y;
	node(LL a,LL b):x(a),y(b){}
};
LL a[100010];
int vis[100010];
vector <node>v[10010];
int n,m,i,j,k,x,y;
queue<int>q;
int main()
{
		scanf("%d%d",&n,&m);
		for (i=1;i<=n;i++)
		{
			scanf("%I64d",&a[i]);
			vis[i]=0;
		}
		if (m==0)
		{
			printf("%I64d\n",a[1]);
			return 0;
		}
		for (i=1;i<=m;i++)
		{
			scanf("%d%d%d",&k,&x,&y);
			v[x].push_back(node(y,k));
			v[y].push_back(node(x,k));
			if (!vis[x])
			{
				vis[x]=1;
				q.push(x);
			}
			if (!vis[y])
			{
				vis[y]=1;
				q.push(y);
			}
		}
		while (!q.empty())
		{
			k=q.front();
			vis[k]=0;
			q.pop();
			int l=v[k].size();
			for (i=0;i<l;i++)
			{
				if (a[k]+a[v[k][i].x]<a[v[k][i].y])
				{
					a[v[k][i].y]=a[k]+a[v[k][i].x];
					if (!vis[v[k][i].y])
					{
						q.push(v[k][i].y);
						vis[v[k][i].y]=1;
					}
				}
			}
		}
		printf("%I64d\n",a[1]);
		return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值