算法与数据结构实验题 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. 【读书笔记】读《JavaScript设计模式》之桥接模式

    桥接模式(Bridge)将抽象部分与它的实现部分分离,使它们都可以独立地变化. 一.使用场景 使用场景一:事件监控 对于前端而言,最典型的使用场景——事件监控.如—— addEvent(element ...

  2. Zabbix discoverer processes more than 75% busy

    [root@86 ~]# grep -n "StartDiscoverers" /usr/local/zabbix/etc/zabbix_server.conf 176:### O ...

  3. ubuntu 安装zabbix_agent端

    root@(none):~# apt-get install zabbix-agent root@(none):~# vi /etc/zabbix/zabbix_agentd.conf Server= ...

  4. hibernate的sqlQuery自动封装

    1.Query query = session.createSQLQuery("SQL").addEntity(Tree.class); //返回对象   List  list = ...

  5. WebRTC源码分析四:视频模块结构

    转自:http://blog.csdn.net/neustar1/article/details/19492113 本文在上篇的基础上介绍WebRTC视频部分的模块结构,以进一步了解其实现框架,只有了 ...

  6. 通过解析PE头。读取dll模块 和 dll模块函数

    win32 int main(){ //001e1000 ::MessageBox(NULL, TEXT("111"), TEXT("222"), 0); HM ...

  7. 关于Scala JDK与IDEA版本兼容的问题

    文章来自:http://www.cnblogs.com/hark0623/p/4174652.html  转发请注明 我刚装上Scala和IDEA时发现运行代码后总是出现 xxx is already ...

  8. Spring AOP 详解

    AOP使用场景 AOP用来封装横切关注点,具体可以在下面的场景中使用: Authentication 权限 Caching 缓存 Context passing 内容传递 Error handling ...

  9. HealthKit开发快速入门教程之HealthKit开发概述简介

    HealthKit开发快速入门教程之HealthKit开发概述简介 2014年6月2日召开的年度开发者大会上,苹果发布了一款新的移动应用平台,可以收集和分析用户的健康数据.该移动应用平台被命名为“He ...

  10. 状态压缩 UVALive 6068 The Little Girl who Picks Mushrooms (12长春C)

    题目传送门 题意:采蘑菇.现在采了n座山,共5座山,最后要求有三个篮子的蘑菇量是1024的整数倍,丢掉后一直减1024直到不超过1024 分析:n <= 3时直接1024,否则状压枚举哪三个篮子 ...