ACM: 图论题 poj 3…

对参加ACM竞赛非常有用的几个ACM关于图论的答题模板 对参加ACM竞赛非常有用的几个ACM关于图论的答题模板,超有用的,把打出来带到考场肯定非常有用! 立即下载
Problem H: Ideal Path
Description
New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci. Visitors of the labyrinth are dropped from the helicopter to the room number 1 and their goal is to get to the labyrinth exit located in the room number n.

Labyrinth owners are planning to run a contest tomorrow. Several runners will be dropped to the room number 1. They will run to the room number n writing down colors of passages as they run through them. The contestant with the shortest sequence of colors is the winner of the contest. If there are several contestants with the same sequence length, the one with the ideal path is the winner. The path is the ideal path if its color sequence is the lexicographically smallest among shortest paths.

Andrew is preparing for the contest. He took a helicopter tour above New Lostland and made a picture of the labyrinth. Your task is to help him find the ideal path from the room number 1 to the room number n that would allow him to win the contest.

Note

A sequence (a1, a2, . . . , ak) is lexicographically smaller than a sequence (b1, b2, . . . , bk) if there exists i such that ai < bi, and aj = bj for all j < i.

Input

The first line of the input file contains integers n and m —the number of rooms and passages, respectively (2 <= n <= 100 000, 1 <= m <= 200 000). The following m lines describe passages, each passage is described with three integer numbers: ai, bi, and ci — the numbers of rooms it connects and its color (1 <= ai, bi <= n, 1 <= ci <= 109). Each passage can be passed in either direction. Two rooms can be connected with more than one passage, there can be a passage from a room to itself. It is guaranteed that it is possible to reach the room number n from the room number 1.

Output

The first line of the output file must contain k — the length of the shortest path from the room number 1 to the room number n. The second line must contain k numbers — the colors of passages in the order they must be passed in the ideal path.

Sample Input

4 6
1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1

Sample Output

2
1 3

 

题意: 一个图, 求出最短路径长度, 并且输出字典序最小的路径. 因为每条路径有一个

      color, 长度都为1.

 

解题思路:

     1. 因为每条路的权值都为1, 最短路径问题. spfa算法改改快速就可以求出.

     2. 设dist[i]表示第i个节点到第n个节点的最短路径. 因此输出最小字典序才是难点.

     问题分析:

        (1). dist[1]: 就是1->n的最短路径. 因为每条边的权值为1, 因此我们把每减小1

             就有一系列的节点, 相当于这条路径的一层(当然路径要连通), 每一层在建

             一个图, 相同dist[i]在同一层. 每一层求出最小的color即可.

        (2). 为了保证路径连通性, 选出来的最小值的节点, 要标记.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define MAX 110005
#define MAXSIZE 400005
const int INF = (1<<30);

struct node
{
 int v;
 int color;
 int next;
}edges1[MAXSIZE];

struct pNode
{
 int v;
 int next;
}edges2[MAXSIZE];

int n, m;
int dist[MAX];
int first1[MAXSIZE], first2[MAXSIZE];
int num1, num2;
bool vis[MAX];

inline int min(int a, int b)
{
 return a < b ? a : b;
}

inline void add1(int u, int v, int color)
{
 edges1[num1].v = v;
 edges1[num1].color = color;
 edges1[num1].next = first1[u];
 first1[u] = num1++;
}

inline void add2(int level, int v)
{
 edges2[num2].v = v;
 edges2[num2].next = first2[level];
 first2[level] = num2++;
}

void read_graph()
{
 memset(edges1, -1, sizeof(edges1));
 memset(edges2, -1, sizeof(edges2));
 memset(first1, -1, sizeof(first1));
 memset(first2, -1, sizeof(first2));
 memset(dist, -1, sizeof(dist));
 memset(vis, false, sizeof(vis));
 num1 = num2 = 0;

 int u, v, color;
 for(int i = 1; i <= m; ++i)
 {
  scanf("%d %d %d", &u, &v, &color);
  add1(u, v, color);
  add1(v, u, color);
 }
}

void spfa()
{
 dist[n] = 0;
 queue qu;
 qu.push(n);
 while( !qu.empty() )
 {
  int u = qu.front();
  qu.pop();
  for(int e = first1[u]; e != -1; e = edges1[e].next)
  {
   if( dist[ edges1[e].v ] == -1 )
   {
    dist[ edges1[e].v ] = dist[u]+1;
    qu.push( edges1[e].v );
   }
  }
 }
}

void printPath()
{
 printf("%d\n", dist[1]);
 int i, e1, e2;
 for(i = 1; i <= n; ++i)
  add2(dist[1]-dist[i], i);
 vis[1] = true;

 int result;
 int temp = 0;
 for(i = 0; i < dist[1]; ++i)
 {
  result = INF;
  for(e2 = first2[i]; e2 != -1; e2 = edges2[e2].next)
  {
   if( vis[ edges2[e2].v ] )
   {
    int v = edges2[e2].v;
    for(e1 = first1[v]; e1 != -1; e1 = edges1[e1].next)
    {
     if( dist[ edges1[e1].v ] == dist[v]-1 )
     {
      result = min(result, edges1[e1].color);
     }
    }
   }
  }

  if(i != 0) printf(" %d", result);
  else printf("%d", result);
  if(i == dist[1]-1) break;

  for(e2 = first2[i]; e2 != -1; e2 = edges2[e2].next)
  {
   if( vis[ edges2[e2].v ] )
   {
    int v = edges2[e2].v;
    for(e1 = first1[v]; e1 != -1; e1 = edges1[e1].next)
    {
     if( dist[ edges1[e1].v ] == dist[v]-1 && result == edges1[e1].color)
     {
      vis[ edges1[e1].v ] = true;
     }
    }
   }
  }
 }
 printf("\n");
}

int main()
{
// freopen("input.txt", "r", stdin);
 while( scanf("%d %d", &n, &m) != EOF )
 {
  read_graph();
  spfa();
  printPath();
 }
 return 0;
}


 

acm pku spoj sgu 经典 图论题解题报告 hdu 1914稳定婚姻 sgu176 有源汇的上下界 求最小满足的流 poj 2230 递归求欧拉回路 poj 2985 bst模板 poj2723 2-sat验证,二分答案 poj2455 dinic (ek会超时) hdu1689 求最小奇数环 poj2391 isap最快,dinic不减枝会超时 poj2455 isap 最快(ek会超时) poj2832 并查集边的计算 sgu218 hcraft 二分图匹配验证 二分答案 立即下载

相关推荐

经典ACM图论问题讲解

这是关于ACM图论问题的经典讲解,简洁精辟的讲解了常见的ACM图论问题!

【算法竞赛入门经典】6.5[图的BFS] 例题6-20 UVa1599

【算法竞赛入门经典】6.5[图的BFS] 例题6-20 UVa1599 算法竞赛入门经典65图的BFS 例题6-20 UVa1599 例题UVa1599 思路 本题储存结构 样例实现代码 例题UVa1599 New labyrinth attraction is open in New Lostland amusement park. The labyrinth con

企鹅崽博客 432

Multisim的探针(Probe)及数字电路的一些调试技巧的应用

Multisim的探针(Probe)及数字电路的一些调试技巧的应用 --EE Bang   By  WRS 上图为555方波发生器,根据R1,R2,C2参数的配置可产生频率为46.3KHZ,占空比50%的方波信号,如果用示波器查看波形参数的话需要用两个坐标轴做差,操作不便,另外如果要修改电路将上述电阻换成滑动变阻器的话,使其产生可变频率

Agent Christmas的博客 3万+

ACM:&nbsp;二分图最佳匹配&nbsp;图论题&nbsp;poj&nbsp;3…

The Windy's Description The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The manager knows that every order wi

smile_benson 316

Windows 11 RDP Wrapper 配置教程与常见问题解决方法

解锁家庭版RDP主机功能(Windows 11家庭版原生不支持作为RDP服务器)突破单用户连接限制,实现多用户同时远程登录(类似服务器版功能)它不修改系统核心文件,通过注入方式工作,相对安全可靠。RDP Wrapper是Windows 11家庭版用户启用远程桌面、突破单用户限制的高效工具。遵循本文步骤可快速完成配置,遇到问题时按常见问题排查方案逐一解决,多数问题可通过更新配置文件或重启服务解决。若长期使用,建议关注官方GitHub仓库的更新,确保兼容性和安全性。

weixin_41521681的博客 1396

openvpn 安装 (Alibaba Cloud Linux release 3 (Soaring Falcon))

1.错误:configure: error: libnl-genl-3.0 package not found or too old. Is the development package and pkg-config (/usr/bin/pkg-config) installed?# ta.key在/etc/openvpn/server/目录下可以如下配置,否则需要写完整路径。# 配置账号密码登录客户端需要ca证书(可选)# 证书校验算法 配置上面生成的完整路径。# ca证书 配置上面生成的完整路径。

dubhe_zhao的博客 2154

ACM:&nbsp;图论题&nbsp;poj&nbsp;1…

COURSES   Description Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exac

smile_benson 319

手把手式介绍 ADNI 影像数据下载

1.搜索 1.1登录 打开ADNI的官网:http://adni.loni.usc.edu/,进入下面的界面。 点击 DATA&SMPLES 下的 ACCESS DATA AND SAMPLES,如下图。 页面跳转以后点击中间的“LOGIN”,这时会跳出一个新的界面,然后在右上角输入自己的帐号、密码即可。如果没有权限可以跳转到第2部分看看如何申请权限。 登录以后选择DOWNLOAD下的 Image Collections就是搜索下载影像数据,其他还有一些学习测试数据和遗传数据。 1.2 sea

永远想不起来更新的博客 1万+

ACM:&nbsp;有向欧拉图&nbsp;图论题&nbsp;poj&nbsp;1386&nbsp;…

Play on Words Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the door

smile_benson 502

WSL 运行make提示/lib/modules/xxx/build: No such file or directory. Stop.错误解决办法

WSL运行make提示/lib//modules/xxx文件或目录不存在。 WSL2的内核是修改过的,无法使用 ubuntu上游的内核头文件和modules文件,因此,我们需要手动编译并安装一个版本。

weixin_45668903的博客 1万+

医学图像分割评价指标详解:从Dice到MSD的实战解析

本文深入解析医学图像分割的核心评价指标,从最常用的Dice相似系数到衡量边界精度的MSD(最大对称表面距离),通过实战案例与代码演示,系统阐述各指标的计算原理、应用场景及临床意义,帮助读者全面评估模型性能。

weixin_29070721的博客 595

ACM:&nbsp;图论题&nbsp;poj&nbsp;1135

Domino Effect   Description Did you know that you can use domino bones for other things besides playing Dominoes? Take a number of dominoes and build a row by standing them on end with only a small di

smile_benson 284

【华为OD机试真题 新系统】1021、API请求日志去重分析 | 机试真题+思路参考+代码解析(C++、Java、Py、C语言、JS)

【华为OD机试真题 新系统】1021、API请求日志去重分析 | 机试真题+思路参考+代码解析(C++、Java、Py、C语言、JS) 华为od机试,华为od机试真题,华为od机试E卷,华为odE卷,华为od机试真题E卷,华为OD,华为OD机试,华为OD机试真题,华为od机考,华为od机试D卷,华为od机试真题D卷,华为od手撕代码,华为od,华为od机试C卷,华为od e卷,华为2026e卷,od机试,2026E卷,华为OD机试E卷,什么是华为od,华为od介绍,全面了解华为od,2026华为od,如何备

KJ.JK 148

ACM:&nbsp;图论题&nbsp;poj&nbsp;1161&nbsp;(把图重建成…

Walls   Description In a country, great walls have been built in such a way that every great wall connects exactly two towns. The great walls do not cross each other. Thus, the country is divided into

smile_benson 300

22、忆阻器、忆阻系统及其神经形态应用综述

本文综述了忆阻器与忆阻系统的理论基础及其在神经形态系统中的广泛应用。从忆阻器的基本定义出发,介绍了其在非线性电路、存储技术、通信和神经形态计算等领域的应用潜力。重点探讨了基于忆阻器的神经元模型(如Hodgkin-Huxley、FitzHugh-Nagumo和Hindmarsh-Rose模型)、突触实现方式及多种忆阻神经网络架构,包括细胞神经网络、脉冲神经网络和递归神经网络等。此外,深入分析了忆阻器在水库计算中的理论研究与硬件实现进展,展示了其在时间序列处理、模式识别和信号预测等方面的优异性能。尽管忆阻技术前

kiwi8的博客 208

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

ACM:&nbsp;图论题&nbsp;poj&nbsp;3308&nbsp;最大流问题

Paratroopers   Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the in

smile_benson 402

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

ACM:&nbsp;图论题&nbsp;poj&nbsp;3352&nbsp;双连通题

Road Construction   Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island pa

smile_benson 412

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

ACM:&nbsp;广搜&nbsp;图论题&nbsp;poj&nbsp;1708

Game   Description A child has drawn N (N<=100) numbered circles of different colors. He has connected some of the circles by colored oriented line segments. Every pair of circles may have any number

smile_benson 382

ACM:&nbsp;图论题&nbsp;poj&nbsp;2253&nbsp;(poj上的dou…

Frogger Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and f

smile_benson 380

ACM:&nbsp;图论题&nbsp;poj&nbsp;1201&nbsp;差分约束

Intervals   Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points and integers c1, ..., cn fr

smile_benson 275

ACM:&nbsp;图论题&nbsp;poj&nbsp;1466&nbsp;最大独立团

Girls and Boys   Description In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one g

smile_benson 446

ACM:&nbsp;匈牙利算法&nbsp;图论题&nbsp;poj&nbsp;3020

Antenna Placement Description The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the jo

smile_benson 297

ACM:&nbsp;差分约束&nbsp;图论题&nbsp;poj&nbsp;2983&nbsp;sp…

Is the Information Reliable? Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Draco established a line of defense called Grot. Grot is a straigh

smile_benson 346

ACM:&nbsp;图论题&nbsp;poj&nbsp;1236&nbsp;强连通

Network of Schools   Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distri

smile_benson 329
上一篇: 兴趣学习:&nbsp;opencv&nbsp;实现画图程序
下一篇: ACM:&nbsp;动态规划题&nbsp;poj&amp;nb…
Smile_Benson
博客等级 码龄16年 9粉丝 354原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值