题目链接

 #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)的更多相关文章

  1. ( 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 ...

  2. 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 ...

  3. HDU 5726 GCD (2016 Multi-University Training Contest 1)

      Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description Give y ...

  4. 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 ...

  5. HDU 4906 Our happy ending(2014 Multi-University Training Contest 4)

    题意:构造出n个数 这n个数取值范围0-L,这n个数中存在取一些数之和等于k,则这样称为一种方法.给定n,k,L,求方案数. 思路:装压 每位 第1为表示这种方案能不能构成1(1表示能0表示不能)   ...

  6. (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 ...

  7. hdu6315( 2018 Multi-University Training Contest 2)

    bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6315 /*hdu 1007 首先我们在建立线段树之前应该思考的是线段树的节点维护一个什么 ...

  8. hdu6314( 2018 Multi-University Training Contest 2)

    bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6314 ----. 又是一个数学题! 这个题使用容斥原理解决的,现场看dls推公式. 我也 ...

  9. 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操 ...

随机推荐

  1. Convolutional Neural Networks for Visual Recognition 1

    Introduction 这是斯坦福计算机视觉大牛李菲菲最新开设的一门关于deep learning在计算机视觉领域的相关应用的课程.这个课程重点介绍了deep learning里的一种比较流行的模型 ...

  2. [原]NYOJ-数的位数-69

    大学生程序代写 /*  NYOJ69 阶乘数位长度 http://acm.nyist.net/JudgeOnline/problem.php?pid=69 数的长度 时间限制:3000 ms  |   ...

  3. ffmpeg代码实现自定义encoder

    1.概述 本文主要讲述如何用ffmpeg代码实现自己的encoder. 2.代码 /* *本程序主要实现一个自己的encoder并加入到encoder链中去,供api调用 *作者:缪国凯(MK) *8 ...

  4. [冬令营模拟]GTSG2018

    上学期没有去 GTSG,于是今天老师让我们来做一下 GTSG2018 Day1 & Day3 Day1 在上午当成一场考试来搞了,Day3 由于锅太多而且 T3 玄学而被放到下午自学... 上 ...

  5. ACM学习历程—BestCoder Round #75

    1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #inclu ...

  6. Poj 1887 Testing the CATCHER(LIS)

    一.Description A military contractor for the Department of Defense has just completed a series of pre ...

  7. POJ 1664 放苹果(递归或DP)

    一.Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t ...

  8. Azure xplate cli创建虚拟机

    Azure的管理工具多种多样.其中Portal和PowerShell是经常使用的. 但用MAC和Linux的同学最喜欢的还是Bash环境. Azure也提供了跨平台的命令行工具:Azure xplat ...

  9. tar 排除某个目录

    tar -zcvf tomcat.tar.gz --exclude=tomcat/logs --exclude=tomcat/libs tomcat

  10. 11_adb指令练习

    通过adb指令咱们装相关的项目.把项目推到设备上.也可以进行文件相关的操作.adb的一些相关的指令. 开启连接IDE和设备的服务. adb可以安装应用也可以卸载应用.项目怎么去区分?一个包名一个是签名 ...