题目链接

 #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. 动作基类 CCAction

    CCAction动作基类关系 class CC_DLL CCAction : public CCObject { public: CCAction(void); virtual ~CCAction(v ...

  2. 【遍历二叉树】02二叉树的中序遍历【Binary Tree Inorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的中序遍历的 ...

  3. ffmpeg混音(将多个声音合成一个)命令

    ffmpeg命令中可以使用filter amix实现这个功能. 官方文档 http://ffmpeg.org/ffmpeg-filters.html 6.8 amix Mixes multiple a ...

  4. HNOI2008玩具装箱 斜率优化

    题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...

  5. java将白色背景图片转换成透明图片

    package evecom.image; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.Buffe ...

  6. oracle数据库规划建议

    之前负责的项目有用到oracle的,oracle dba给过一些建议,自己整理了一下,写再这里做个备忘 数据库需求分析: 1. 创建的数据库名称为maildb,并且字符集为UTF8. 2. 提供可连接 ...

  7. 1115. Counting Nodes in a BST (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  8. CH5103 [NOIP2008]传纸条[线性DP]

    给定一个 N*M 的矩阵A,每个格子中有一个整数.现在需要找到两条从左上角 (1,1) 到右下角 (N,M) 的路径,路径上的每一步只能向右或向下走.路径经过的格子中的数会被取走.两条路径不能经过同一 ...

  9. ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...

  10. 在屏幕中间显示,按ESC键后改变字符的颜色

    程序功能:编写程序在屏幕中间显示“a”~“z”,并可以让人看清,这个任务比较好实现. (1)在b800:[ 160*12+40*2]处存入a的ASCII码.(2)在循环中使用一个10000000000 ...