DS实验题 sights
算法与数据结构实验题 6.3 sights
★实验任务
美丽的小风姑娘打算去旅游散心,她走进了一座山,发现这座山有 n 个景点, 由于山路难修,所以施工队只修了最少条的路,来保证 n 个景点联通,娇弱的小 风姑娘不想走那么长的山路,所以打算乘坐专用的交通工具。有的景点之间有路, 乘坐交通工具需要花费一定的金额。
由于到达景区之前已经花了一部分钱了,现在可爱的小风姑娘站在景点 1, 即根景点。按原计划她要去编号为 m 的景点,导游告诉她到景点 m 总共要花的钱 (包括来之前花的钱)。然而善变的小风姑娘突然想去景点 y(直接从景点 1 到 景点 y),所以她想要知道到景点 y 总共要花多少钱,作为程序员之花,她想用 代码来解决这个问题。
★数据输入
输入第一行为一个正整数 n 表示景点的数目,景点从 1 到 n 编号。
接下来 n-1 行,代表山路,每行三个整数 x,y,p,分别表示 x,y 景点间的路 费。
第 n+1 行一个整数 q 表示询问组数。每组数据独立互不相关。
紧跟着 q 行,每行三个整数 m,v,y,分别表示 m 景点的编号,到达 m 景点 的总花费 v,以及要求的 y 景点。
30%的数据 n<=20,p<=100,q<=10 70%的数据 n<=1000,p<=10000,q<=100 100%的数据 n<=100000,p<=1000000,q<=n
★数据输出
输出 q 行,每行一个整数表示题目要求的答案,由于答案可能很大,输出对 707063423 取余的结果。
输入示例:
4
1 2 5
1 3 6
2 4 4
2 284 362
输出示例
12 5
全WA代码,思路是最简单的建树:
//
// main.cpp
// sights
//
// Created by wasdns on 16/10/16.
// Copyright © 2016年 wasdns. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;
struct Tree {
Tree* l;
Tree* r;
int data;
int lcost;
int rcost;
};
struct nodes {
int a;
int b;
int cost;
bool flag;
}Node[100000];
Tree* Initial() {
Tree* header;
header = new Tree;
header -> l = NULL;
header -> r = NULL;
header -> data = -1;
header -> lcost = -1;
header -> rcost = -1;
return header;
}
int NodeCost(Tree* header, int Nodedata) {
queue<Tree*> q;
queue<int> int_q;
int_q.push(0);
Tree* turn;
q.push(header);
while (!q.empty()) {
turn = q.front();
q.pop();
int a = int_q.front();
if (turn -> data == Nodedata) {
return a;
}
if (turn -> l != NULL) {
if (turn -> l -> data != -1) {
q.push(turn -> l);
int_q.push(a + turn -> lcost);
}
}
if (turn -> r != NULL) {
if (turn -> r -> data != -1)
q.push(turn -> r);
int_q.push(a + turn -> rcost);
}
int_q.pop();
}
return -1;
};
Tree* CreatTree(Tree* header, int n) {
header = Initial();
queue<Tree*> q;
header -> data = 1;
q.push(header);
while (1)
{
int turn;
if (q.empty()) {
break;
}
Tree* tpoint = q.front();
turn = tpoint -> data;
q.pop();
int flag = 0; //有两个子节点
for (int i = 1; i <= n - 1; i++) {
if ( !Node[i].flag &&
( Node[i].a == turn || Node[i].b == turn ) ) { //父子相认
Node[i].flag = true;
//cout << "turn: " << turn << endl;
//cout << Node[i].a << " " << Node[i].b << endl;
Tree* newpoint;
newpoint = Initial();
if (Node[i].a == turn) {
newpoint -> data = Node[i].b;
}
else {
newpoint -> data = Node[i].a;
}
if (flag) {
tpoint -> r = newpoint;
tpoint -> rcost = Node[i].cost;
}
else {
tpoint -> l = newpoint;
tpoint -> lcost = Node[i].cost;
flag = 1;
}
q.push(newpoint);
}
}
}
return header;
}
void PrintTree(Tree* p) {
queue<Tree*> q;
q.push(p);
Tree* turn;
turn = Initial();
while (!q.empty()) {
turn = q.front();
q.pop();
cout << turn -> data << " ";
if (turn -> l != NULL) {
if (turn -> l -> data != -1)
q.push(turn -> l);
}
if (turn -> r != NULL) {
if (turn -> r -> data != -1)
q.push(turn -> r);
}
}
cout << endl;
}
int main() {
int n, i;
cin >> n;
for (i = 1; i <= n - 1; i++) {
cin >> Node[i].a >> Node[i].b >> Node[i].cost;
Node[i].flag = false;
}
Tree* header;
header = CreatTree(header, n);
//PrintTree(header);
int asktime;
cin >> asktime;
int pre, post, precost;
for (i = 1; i <= asktime; i++) {
cin >> pre >> precost >> post;
int mid = precost - NodeCost(header, pre);
cout << NodeCost(header, post) + mid << endl;
}
return 0;
}
AC代码,朝夕的做法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef __int64 LL;
const int maxn = 100010;
const LL mod = 707063423;
const LL INF = 0xffffffff;
struct Edge{
int u,v,next;
LL w;
}edge[2*maxn];
int tot = 0,head[maxn];
LL dis[maxn];
bool vis[maxn];
void addedge(int u,int v,LL w)
{
edge[tot].u = u;edge[tot].v = v;edge[tot].w = w;edge[tot].next = head[u];
head[u] = tot++;
}
void spfa(int N)
{
int i;
memset(vis,false,sizeof(vis));
for (i = 0;i <= N;i++) dis[i] = INF;
queue<int>que;
while (!que.empty()) que.pop();
dis[1] = 0;
que.push(1);
vis[1] = true;
while (!que.empty())
{
int u = que.front();
que.pop();
vis[u] = false;
for (i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if (dis[u] + edge[i].w < dis[v])
{
dis[v] = dis[u] + edge[i].w;
if (!vis[v])
{
que.push(v);
vis[v] = true;
}
}
}
}
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("2.txt","w",stdout);
int N,u,v,q,m,y,i;
LL w,cv;
memset(head,-1,sizeof(head));
scanf("%d",&N);
for (i = 0;i < N - 1;i++)
{
scanf("%d%d%I64d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
spfa(N);
scanf("%d",&q);
while (q--)
{
scanf("%d%I64d%d",&m,&cv,&y);
cv = (cv - dis[m] + mod)%mod;
printf("%I64d\n",(dis[y]%mod + cv%mod)%mod);
}
return 0;
}
可以参考下朝夕的博客:zzy19961112
2016/10/16
DS实验题 sights的更多相关文章
- DS实验题 融合软泥怪-2 Heap实现
题目和STL实现:DS实验题 融合软泥怪-1 用堆实现优先队列 引言和堆的介绍摘自:Priority Queue(Heaps)--优先队列(堆) 引言: 优先队列是一个至少能够提供插入(Insert) ...
- DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储
题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...
- DS实验题 Dijkstra算法
参考:Dijkstra算法 数据结构来到了图论这一章节,网络中的路由算法基本都和图论相关.于是在拿到DS的实验题的时候,决定看下久负盛名的Dijkstra算法. Dijkstra的经典应用是开放最短路 ...
- DS实验题 order
算法与数据结构 实验题 6.4 order ★实验任务 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后序遍历. ★数据输入 输入第一行为一个正整数n表示二叉树的节点数目,节点编号从 ...
- DS实验题 Order 已知父节点和中序遍历求前、后序
题目: 思路: 这题是比较典型的树的遍历问题,思路就是将中序遍历作为位置的判断依据,假设有个节点A和它的父亲Afa,那么如果A和Afa的顺序在中序遍历中是先A后Afa,则A是Afa的左儿子,否则是右儿 ...
- DS实验题 Inversion
题目: 解题过程: 第一次做这题的时候,很自然的想到了冒泡和选择,我交的代码是用选择写的.基本全WA(摊手). 贴上第一次的代码: // // main.cpp // sequenceschange ...
- DS实验题 Missile
题目: 提示:并没有精度问题. 原题 NOIP2010 导弹拦截 思路 设源点为A(x1, y1)和B(x2, y2). 第一步,用结构体存节点,包括以下元素: 1.横坐标x 2.纵坐标y 3.节点和 ...
- DS实验题 击鼓传花
题目: 代码1(数组实现): // // main.cpp // DS-击鼓传花 // // Created by wasdns on 16/11/9. // Copyright © 2016年 wa ...
- DS实验题 地鼠安家
★实验任务 fd是一个公认的美丽校园.一天,fd来了一群地鼠,编号为1到n,他们希望在这里定居.现在先由第一只地鼠往下打一个单位的距离,并且在那里安家.对于每一个已经安家的地鼠,如果他左下或右下没有邻 ...
随机推荐
- Android中mesure过程详解
我们在编写layout的xml文件时会碰到layout_width和layout_height两个属性,对于这两个属性我们有三种选择:赋值成具体的数值,match_parent或者wrap_conte ...
- PW试验-----verilog
PWM,脉冲宽度调制.顾名思义,是通过调制脉冲的宽度,即占空比,来实现的.可是使占空比逐渐由最小增加到最大,也可以使占空比由最大减少到最小来实现不同的现象.若用LED灯来显示现象,则可以称作:LED呼 ...
- svn 结合rsync 的代码发布系统
由开发提交到测试环境,经测试,在由运维统一上线.试验需求一台测试服务器,一台线上(生产环境)服务器.测试服务器上跑svn是开发用于代码管理,而线上跑的svn是运维用来代码上线的.结合rsync保持测试 ...
- Codeigniter CRUD代码快速构建
一个与数据库操作打交道的应用,必然涉及到数据的添加.修改.删除等操作.因此CRUD操作几乎成为每个后台管理站点的必备功能.数据库的复杂性,导致PHP操作代码也会有不少的冗余,因此,如果可以有工具自动生 ...
- kinect学习笔记(三)——深度数据的提取
一.创建Console工程 二.添加kinect引用 里面用引用,打开后 选择然后OK. 三.编写代码(有附加注释) using System; using System.Collections.Ge ...
- jquery概要--基础02
复制节点:clone();默认不会复制绑定事件,如果传入参数true会复制:替换节点: replaceWith() //原节点放在前,新节点放在在后: replaceAll( ...
- ajax的参数
http://www.w3school.com.cn/jquery/ajax_ajax.asp call.addAllremark = function(data){ $.ajax({ url:cal ...
- 状压DP POJ 2411 Mondriaan'sDream
题目传送门 /* 题意:一个h*w的矩阵(1<=h,w<=11),只能放1*2的模块,问完全覆盖的不同放发有多少种? 状态压缩DP第一道:dp[i][j] 代表第i行的j状态下的种数(状态 ...
- 计算几何 2013年山东省赛 A Rescue The Princess
题目传送门 /* 已知一向量为(x , y) 则将它旋转θ后的坐标为(x*cosθ- y * sinθ , y*cosθ + x * sinθ) 应用到本题,x变为(xb - xa), y变为(yb ...
- 2015ACM/ICPC亚洲区长春站 L hdu 5538 House Building
House Building Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...