NEERC 2013 Dwarf Tower (最短路)
2014-11-12 14:28
225 查看
Problem D. Dwarf Tower
Input file: dwarf.in
Output file: dwarf.out
Time limit: 2 seconds
Memory limit: 256 megabytes
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 ≤ 10
9
).
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.
Examples
dwarf.in dwarf.out
5 3
5 0 1 2 5
5 2 3
4 2 3
1 4 5
2
3 1
2 2 1
1 2 3
2
a,b可以交换c,则a->c,距离c[b],b->c,距离为c[a],再加一个超级源点连向每个点,距离为c[i],然后从源点跑最短路,求出到每个点的最短距离。这个最短距离就是通过交换或不换买到物品i的最小代价。有了这些代价后,枚举得到1的方案, 求最小值即可。
Input file: dwarf.in
Output file: dwarf.out
Time limit: 2 seconds
Memory limit: 256 megabytes
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 ≤ 10
9
).
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.
Examples
dwarf.in dwarf.out
5 3
5 0 1 2 5
5 2 3
4 2 3
1 4 5
2
3 1
2 2 1
1 2 3
2
a,b可以交换c,则a->c,距离c[b],b->c,距离为c[a],再加一个超级源点连向每个点,距离为c[i],然后从源点跑最短路,求出到每个点的最短距离。这个最短距离就是通过交换或不换买到物品i的最小代价。有了这些代价后,枚举得到1的方案, 求最小值即可。
#include<cstdio> #include<map> #include<queue> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<list> #include<set> #include<cmath> using namespace std; const int maxn = 1e4 + 5; const int INF = 1e9; const double eps = 1e-6; typedef unsigned long long ULL; typedef long long LL; typedef pair<LL, LL> P; #define fi first #define se second LL c[maxn]; int n; vector<P> G[maxn]; priority_queue<P> q; LL dis[maxn]; void dij(){ while(!q.empty()) q.pop(); for(int i = 1;i <= n;i++) dis[i] = INF; q.push(P(0, 0)); dis[0] = 0; while(!q.empty()){ P tem = q.top(); q.pop(); LL total = -tem.fi; int pos = tem.se; if(total > dis[pos]) continue; for(int i = 0;i < G[pos].size();i++){ P edge = G[pos][i]; int to = edge.fi; LL der = edge.se; if(total+der < dis[to]){ dis[to] = total + der; q.push(P(-dis[to], to)); } } } } P edge[maxn*10]; int main(){ freopen("dwarf.in", "r", stdin); freopen("dwarf.out", "w", stdout); int m; while(scanf("%d%d", &n, &m) != EOF){ for(int i = 0;i <= n;i++) G[i].clear(); for(int i = 1;i <= n;i++){ scanf("%I64d", &c[i]); G[0].push_back(P(i, c[i])); } int cnt = 0; while(m--){ int to, x, y; scanf("%d%d%d", &to, &x, &y); G[x].push_back(P(to, c[y])); G[y].push_back(P(to, c[x])); if(to == 1){ edge[cnt++] = P(x, y); } } dij(); LL ans = dis[1]; for(int i = 0;i < cnt;i++){ int x = edge[i].fi; int y = edge[i].se; ans = min(ans, dis[x]+dis[y]); } printf("%I64d\n", ans); } return 0; }
相关文章推荐
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem I. Plugs and Sockets 费用流
- HNU 12847 Dwarf Tower(最短路+队列优化)
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem L. Stock Trading Robot 水题
- 2012-2013 ACM-ICPC, NEERC, Moscow Subregional Contest
- 2013-2014 ACM-ICPC, NEERC, Moscow Subregional Contest (2013区域赛练习)
- NOIP2013 华容道 (BFS,最短路)
- JZOJsenior3470.【NOIP2013模拟联考8】最短路(path)
- 2013寒假ACM集训_最短路
- HOJ 12847 Dwarf Tower (最短路)
- NEERC 2013, Eastern subregional contest
- 2013校队选拔——最短路——二分最大边的最小值
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest C Sequence (打表)
- 总结(【中山市选2014】投票 【中山市选2014】dwarf tower 【中山市选2014】图 【NOIP2013提高组day2】华容道)
- 2012-2013 Waterloo Local Contest, 13 October, 2012 A题(最短路变形)
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest H Milestones1 (暴力)
- vijos1846 [NOIP2013] 华容道【最短路】
- 2012-2013 ACM-ICPC, NEERC, Southern Subregional Contest 解题报告
- ACM ICPC 2013-2014. NEERC. Eastern Subregional Contest
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)