算法与数据结构实验题 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的更多相关文章

  1. DS实验题 融合软泥怪-2 Heap实现

    题目和STL实现:DS实验题 融合软泥怪-1 用堆实现优先队列 引言和堆的介绍摘自:Priority Queue(Heaps)--优先队列(堆) 引言: 优先队列是一个至少能够提供插入(Insert) ...

  2. DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储

    题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...

  3. DS实验题 Dijkstra算法

    参考:Dijkstra算法 数据结构来到了图论这一章节,网络中的路由算法基本都和图论相关.于是在拿到DS的实验题的时候,决定看下久负盛名的Dijkstra算法. Dijkstra的经典应用是开放最短路 ...

  4. DS实验题 order

    算法与数据结构 实验题 6.4 order ★实验任务 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后序遍历. ★数据输入 输入第一行为一个正整数n表示二叉树的节点数目,节点编号从 ...

  5. DS实验题 Order 已知父节点和中序遍历求前、后序

    题目: 思路: 这题是比较典型的树的遍历问题,思路就是将中序遍历作为位置的判断依据,假设有个节点A和它的父亲Afa,那么如果A和Afa的顺序在中序遍历中是先A后Afa,则A是Afa的左儿子,否则是右儿 ...

  6. DS实验题 Inversion

    题目: 解题过程: 第一次做这题的时候,很自然的想到了冒泡和选择,我交的代码是用选择写的.基本全WA(摊手). 贴上第一次的代码: // // main.cpp // sequenceschange ...

  7. DS实验题 Missile

    题目: 提示:并没有精度问题. 原题 NOIP2010 导弹拦截 思路 设源点为A(x1, y1)和B(x2, y2). 第一步,用结构体存节点,包括以下元素: 1.横坐标x 2.纵坐标y 3.节点和 ...

  8. DS实验题 击鼓传花

    题目: 代码1(数组实现): // // main.cpp // DS-击鼓传花 // // Created by wasdns on 16/11/9. // Copyright © 2016年 wa ...

  9. DS实验题 地鼠安家

    ★实验任务 fd是一个公认的美丽校园.一天,fd来了一群地鼠,编号为1到n,他们希望在这里定居.现在先由第一只地鼠往下打一个单位的距离,并且在那里安家.对于每一个已经安家的地鼠,如果他左下或右下没有邻 ...

随机推荐

  1. sysctl命令详解

    个人一般sysctl -p 或sysctl -a比较多使用 sysctl配置与显示在/proc/sys目录中的内核参数.可以用sysctl来设置或重新设置联网功能,如IP转发.IP碎片去除以及源路由检 ...

  2. 【解决】 新浪sae固定链接404 问题

    固定链接404 固定链接是一个很重要的内容,wordpress默认的链接很复杂,也不利于搜索引擎搜索.wordpress也提供修改固定链接的功能,在设置里面[固定链接]修改. 但是,使用新浪sae的小 ...

  3. SQL表格

    LAMP - Linux  Apache MySQL PHP MySQL - 三个层次:文件层次,服务层次,界面 常用的数据类型:int 整数float double decimal 小数varcha ...

  4. C#学习笔记---如何提高代码逼格

    水文.如何让自己的代码看起来,更有逼格? 要想让自己的代码,看起来更优雅,更有逼格,更高大上,就一定要写出晦涩难懂,而又简洁的代码来. 对于类自身的全局变量,一定要加this,对于基类的,一定要加ba ...

  5. 项目如何脱离TFS 2010的管理

    在VS 里,文件->源代码管理->更改源代码管理->取消绑定.

  6. Linux常用命令_(基本命令)

    基本命令:ls.cd.pwd.man 1.ls 打印当前目录下的文件和目录文件 用法详解:: ls [-alFR] [文件或目录] -a 显示所有文件,包括隐藏文件:[root@qmfsun]#ls ...

  7. volatile的理解

    用法解释 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修改了某个变量的值,这新值对其 ...

  8. UVa10779 Collectors Problem(最大流)

    很容易想到源点向所类型有贴纸连边,容量为Bob一开始有的数量:然后贴纸向汇点连边,容量为1. 接下来就是交换部分的连边了.注意交换一次一次进行,每次只能交换一张. 交换,是对于两种贴纸而言,仅会发生在 ...

  9. BZOJ2530 : [Poi2011]Party

    注意到随机一组贪心解得到的团的大小不小于$\frac{N}{3}$的概率是很大的,所以一直随机下去,直到找到一组解即可,随机次数是常数级别的,所以复杂度为$O(n^2)$. #include<c ...

  10. BZOJ3780 : 数字统计

    从低位到高位数位DP,f[i][j][k]表示已经填了后i位,转化的数字为j,后i位与x后i位的大小关系为k的方案数. #include<cstdio> const int N=202,B ...