CodeChef - CRYPCUR
AMRExchange is the latest cryptocurrency exchange that has become very popular among cryptocurrency traders.
On AMRExchange, there are N cryptocurrencies - let us denote the ith currency by Ci. Mpairs of these cryptocurrencies are tradable - one unit of currency Cx can be converted to one unit of currency Cy with risk Cxy.
Mr. X, an avid cryptocurrency collector, wants to start with 1 unit of any of the Ncryptocurrencies and perform a sequence of trades. He wants to do it in such a way that for each of the N cryptocurrencies, there was at least one point during the trading sequence during which he held one unit of that cryptocurrency.
The overall risk of the sequence of trades is the maximum risk in the sequence of trades. Minimize the overall risk with which Mr. X can achieve this. Print "Impossible" if no such sequence of trades is possible.
Input
- The first line contains a single integer T - the total number of testcases.
- Each testcase is of the following format:
- First line contains 2 space-separated integers - N and M. N denotes the number of cryptocurrencies, M denotes the number of tradable ordered cryptocurrency pairs.
- M lines follow. Each line contains 3 space-separated positive integers - Cx, Cyand Cxy. This line denotes that one unit of currency Cx can be converted into one unit of currency Cy with risk Cxy.
Output
- For each testcase, print the minimum overall risk with which Mr. X can own at least one unit of each cryptocurrency at some point in time.
- If it is not possible for Mr. X to achieve this, then print “Impossible”.
Constraints
- 1 ≤ T ≤ 5
- 1 ≤ N, M ≤ 105
- 1 ≤ Cx, Cy ≤ N
- 1 ≤ Cxy ≤ 109
Example
Input
2
3 6
1 2 1
2 3 3
3 1 3
1 3 1
3 2 1
3 2 5
4 3
1 2 1
2 3 1
2 4 1 Output
1
Impossible
Explanation
Testcase 1: Mr. X can start with cryptocurrency C1 and follow the following sequence to minimize overall risk:
- Convert C1 to C3 with risk 1.
- Convert C3 to C2 with risk 1.
The overall risk would be 1.
Testcase 2: There are a total of 6 sequences of trades are possible, and none of them satisfy our property. We list them here:
Starting with C1:
- C1 -> C2 -> C3
- C1 -> C2 -> C4
In the first sequence, Mr. X won't be able to own C4 because units of C3 cannot be converted to units of C4. Similarly, in the second sequence, Mr. X won't be able to own C3 because units of C4 cannot be converted to units of C3.
Starting with C2:
- C2 -> C3
- C2 -> C4
Starting with C3:
- C3 (cannot be converted to any other cryptocurrency)
Starting with C4:
- C4 (cannot be converted to any other cryptocurrency)
Hence, there is no possible sequence using which Mr. X can own one unit of all cryptocurrencies at some point in time.
题意
给一个有边权的图,找出最小的权值,使得以这个权值能走遍所有的点。
分析
二分权值。对于某个权值,以这个权值为基准来缩点,构建出新的简单有向图,然后求出拓扑序,检查是否可以形成链状。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
#include<deque>
#include<stack>
using namespace std;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+;
const int mod = +;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
typedef long long ll;
vector<pii> G[maxn];
vector<int> g[maxn];
int indeg[maxn];
int n,m;
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_block,scc_cnt;
stack<int> S;
void dfs(int u,int lim){
pre[u]=lowlink[u]=++dfs_block;
S.push(u);
for(int i=;i<G[u].size();i++){
if(G[u][i].Y>lim) continue;
int v=G[u][i].X;
if(!pre[v]){
dfs(v,lim);
lowlink[u]=min(lowlink[u],lowlink[v]);
}else if(!sccno[v]){
lowlink[u]=min(lowlink[u],pre[v]);
}
}
if(lowlink[u]==pre[u]){
scc_cnt++;
while(){
int x=S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u) break;
}
}
return;
}
void find_scc(int n,int lim){
dfs_block=scc_cnt=;
ms(sccno,);
ms(pre,);
for(int i=;i<=n;i++){
if(!pre[i]) dfs(i,lim);
}
return;
} bool check(int mid){
ms(indeg,);
for(int i=;i<=scc_cnt;i++){
g[i].clear();
}
find_scc(n,mid);
for(int i=;i<=n;i++){
for(int j=;j<G[i].size();j++){
int u,v,w;
u=i;
v=G[i][j].X;
w=G[i][j].Y;
if(w>mid) continue;
if(sccno[u]==sccno[v]) continue;
g[sccno[u]].pb(sccno[v]);
indeg[sccno[v]]++;
}
} queue<int> que;
for(int i=;i<=scc_cnt;i++) if(indeg[i]==) que.push(i);
vector<int> topu;
while(!que.empty()){
int now=que.front();
que.pop();
topu.pb(now);
for(int i=;i<g[now].size();i++){
indeg[g[now][i]]--;
if(indeg[g[now][i]]==){
que.push(g[now][i]);
}
}
} if(topu.size()<=) return true;
for(int i=;i<topu.size()-;i++){
int now=topu[i];
bool flag=;
for(int j=;j<g[now].size();j++){
int tmp=g[now][j];
if(tmp==topu[i+]){
flag=;
break;
}
}
if(!flag){
return false;
}
}
return true;
}
void solve(){
int l=,r=1e9+;
int mid;
int ans=inf;
while(l<=r){
mid=(l+r)>>;
if(check(mid)){
r=mid-;
ans=min(mid,ans);
}else l=mid+;
}
if(ans==inf) puts("Impossible");
else printf("%d\n",ans);
} int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int t,u,v,w;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) G[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
G[u].pb(mp(v,w));
}
solve();
}
return ;
}
CodeChef - CRYPCUR的更多相关文章
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- 【BZOJ4260】 Codechef REBXOR 可持久化Trie
看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...
- codechef 两题
前面做了这场比赛,感觉题目不错,放上来. A题目:对于数组A[],求A[U]&A[V]的最大值,因为数据弱,很多人直接排序再俩俩比较就过了. 其实这道题类似百度之星资格赛第三题XOR SUM, ...
- codechef January Challenge 2014 Sereja and Graph
题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...
- BZOJ3509: [CodeChef] COUNTARI
3509: [CodeChef] COUNTARI Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 339 Solved: 85[Submit][St ...
- CodeChef CBAL
题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...
- CodeChef FNCS
题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...
- codechef Prime Distance On Tree(树分治+FFT)
题目链接:http://www.codechef.com/problems/PRIMEDST/ 题意:给出一棵树,边长度都是1.每次任意取出两个点(u,v),他们之间的长度为素数的概率为多大? 树分治 ...
- BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )
树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...
随机推荐
- laravel5 报错419,form 添加crrf_field 后让然失败,本地环境配置问题
这个是因为laravel自带CSRF验证的问题 解决方法 方法一:去关掉laravel的csrf验证,但这个人不建议,方法也不写出来了. 方法二:把该接口写到api.php上就好了 方法三: 首先在页 ...
- ARIMA模型识别、计算p、q值
#-*- coding: utf-8 -*- #确定最佳p.d.q值 import pandas as pd #参数初始化 discfile = '../data/discdata_processed ...
- Dictionary字典
泛型,键值对 Dictionary<int,string> dic=new Dictionary<int,string>(); dic.Add(,"张三") ...
- Django-website 程序案例系列-2 字典操作
设置一个全局字段: USER_DICT = { 'k1': 'root1', 'k2': 'root2', 'k3': 'root3', } def index(request): return re ...
- BZOJ4386[POI2015]Wycieczki——矩阵乘法+倍增
题目描述 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长度,注意路径不一定是简单路径,即可以重复走同一个点. 输入 第 ...
- BZOJ1042 HAOI2008硬币购物(任意模数NTT+多项式求逆+生成函数/容斥原理+动态规划)
第一眼生成函数.四个等比数列形式的多项式相乘,可以化成四个分式.其中分母部分是固定的,可以多项式求逆预处理出来.而分子部分由于项数很少,询问时2^4算一下贡献就好了.这个思路比较直观.只是常数巨大,以 ...
- [洛谷P4245]【模板】任意模数NTT
题目大意:给你两个多项式$f(x)$和$g(x)$以及一个模数$p(p\leqslant10^9)$,求$f*g\pmod p$ 题解:任意模数$NTT$,最大的数为$p^2\times\max\{n ...
- 机器学习&深度学习资料收集
缘由 以下博客都是我在学习过程中看到的一些知识讲解非常好的博文,就不转载了,直接给出链接方便以后重复访问.有了自己的理解之后再重新整理资料发布吧 : ) sklearn系列 http://www.cn ...
- NHibernate使用简单示例
NHibernate使用小示例 1.新建Model类库项目. 使用代码生成器生成Model类. 此处以简单的UserInfo表作为示例. 注意字段前必须以 virtual 修饰. namespace ...
- CF294C Shaass and Lights
题目大意: 有n盏灯,(0<=n<=1000),有m盏已经点亮,每次只能点亮与已经点亮的灯相邻的灯,求总方案数,答案对1e9+7取模 第一行:两个整数n,m表示灯的总数和已点亮的灯的数目 ...