Age of Moyu (2018 Multi-University Training Contest 7)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read(){
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} /***********************************************************/ const int maxn = ;
int n, m;
int dis[maxn], head[maxn], look[maxn];
queue<int>q;
int ans, cas; struct Edge
{
int u, v, w, next, vis;
}edge[maxn*]; void add_edge(int u, int v, int w){
edge[cas].u = u; edge[cas].v = v; edge[cas].w = w;
edge[cas].next = head[u]; edge[cas].vis = ;
head[u] = cas++;
} void dfs(int rt, int cl, int num){
if(rt == n) {
ans = num;
return;
}
if(!look[rt]){
look[rt] = ;
dis[rt] = num;
q.push(rt);
}
for(int i = head[rt];i != -;i = edge[i].next){
if(edge[i].vis) continue;
if(edge[i].w == cl){
edge[i].vis = ;
dfs(edge[i].v, cl, num);
}
}
return;
} int bfs(){
while(!q.empty()) q.pop();
q.push(); //从一开始走
dis[] = ;
look[] = ;
while(!q.empty()){
int now = q.front(); q.pop();
for(int i = head[now];i != -;i = edge[i].next){
if(edge[i].vis) continue; //每条边只走一次
int v = edge[i].v;
edge[i].vis = ;
dfs(v, edge[i].w, dis[now]+); //把所有与当前路径相同权值的路径加入
if(ans > ) break;
}
if(ans > ) break;
}
return ans;
} void init(){
cas = ;
memset(head, -, sizeof(head));
memset(look, , sizeof(look));
memset(dis, 0x7f, sizeof(dis));
} int main(){
while(~scanf("%d%d", &n, &m)){
init();
for(int i = ;i <= m;i++){
int u, v, w;
u = read(); v = read(); w = read();
add_edge(u, v, w);
add_edge(v, u, w);
}
ans = -;
ans = bfs();
printf("%d\n", ans);
}
return ;
}
Age of Moyu (2018 Multi-University Training Contest 7)的更多相关文章
- ( 2018 Multi-University Training Contest 2)
2018 Multi-University Training Contest 2) HDU 6311 Cover HDU 6312 Game HDU 6313 Hack It HDU 6314 Mat ...
- HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)
Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU 5726 GCD (2016 Multi-University Training Contest 1)
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description Give y ...
- HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 Problem Description There is an old country and ...
- HDU 4906 Our happy ending(2014 Multi-University Training Contest 4)
题意:构造出n个数 这n个数取值范围0-L,这n个数中存在取一些数之和等于k,则这样称为一种方法.给定n,k,L,求方案数. 思路:装压 每位 第1为表示这种方案能不能构成1(1表示能0表示不能) ...
- (2018 Multi-University Training Contest 3)Problem D. Euler Function
//题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6322 //题目大意:给定 k,求第 k 小的数 n,满足 φ(n) 是合数.显然 φ(1) = 1 ...
- hdu6315( 2018 Multi-University Training Contest 2)
bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6315 /*hdu 1007 首先我们在建立线段树之前应该思考的是线段树的节点维护一个什么 ...
- hdu6314( 2018 Multi-University Training Contest 2)
bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6314 ----. 又是一个数学题! 这个题使用容斥原理解决的,现场看dls推公式. 我也 ...
- hdu6313( 2018 Multi-University Training Contest 2)
bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6313 参考dls的讲解: 以5*5的矩阵为例: 后一列分别对前一列+0+1+2+3+4操 ...
随机推荐
- 【二叉查找树】03验证是否为二叉查找树【Validate Binary Search Tree】
本质上是递归遍历左右后在与根节点做判断,本质上是后序遍历 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- bzoj 3752: Hack 预处理+暴力dfs
题目大意: 定义字符串的hash值\(h = \sum_{i=0}^{n-1}p^{n-i-1}s_i\) 现在给定K个长度不超过L的字符串S,对于每个字符串S,求字典序最小长度不超过L的字符串T使得 ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- WPF中Xaml编译正常而Designer Time时出错的解决办法
开发wpf时我们经常遇到一个xaml文件在设计时显示解析错误(比如在:VS或者Blend)而编译正常运行正常. 原因是:xaml的在Debug版本下必须为anyCPU. 解决办法: 1.打开工程文件x ...
- java流类
总结:new FileInputStream package com.ds; import java.io.*; import com.da.fgbv; public class rter { pub ...
- GCC提供的几个內建函数
参考 https://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Other-Builtins.html#Other-Builtins https://en.wikipe ...
- Wix 使用总结(续)--关于Feature和Component的状态判断安装过程(转)
安装过程中,有时候需要根据用户的设置来进行不同的安装,其中一个方面就是根据用户选择安装的Feature或者Component,来判断下一步的操作. Wix中提供了相关的判断方法和内置的状态值. ...
- Spring Boot 专栏
http://blog.csdn.net/column/details/spring-boot.html?&page=2
- PHP二维数组,根据多个字段来排序
如果是最最常见的二维数组排序, 大多数情况下也只用到二维: 用php内置函数 array_multisort( ) 是最简单的: <?php 假设, $arr 是一个二维数组, $arg1是取 ...
- hadoop mapreduce 计算平均气温的代码,绝对原创
1901 46 1902 21 1903 48 1904 33 1905 43 1906 47 1907 31 1908 28 1909 26 1910 35 1911 30 1912 16 1913 ...