ds 6.3 sight
2016-12-08 00:28
197 查看
数组实现邻接表(参考博客)
/***************
Problem from :
Problem describe :
data:
****************/
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<stack>
#include<queue>
#include<ctime>
#include<cstring>
#include<vector>
#include<string>
#define ll __int64
#define inf 0x3f3f3f3f3f
using namespace std;
const int maxn = 100055;
const int mod = 707063423;
int u[2*maxn], v[2*maxn], w[2*maxn], first[maxn], next[2*maxn];
void init()
{
memset(first, -1, sizeof(first));
}
void addedge(int i, int u)//数组实现邻接表 (存边)
{
next[i] = first[u];
first[u] = i;
return ;
}
int dis[maxn]={0}, vis[maxn]={0}; //dis 距离 vis 是否用过
void bfs(int root) //求出各点到根节点的距离
{
queue<int>que;
dis[root] = 0;
vis[root]=1;
que.push(root);
while(!que.empty())
{
int p = que.front();
que.pop();
for(int i=first[p]; i!=-1; i=next[i])
{
int q = v[i];
if(!vis[q])
{
vis[q] = 1;
dis[q] = (w[i]+ dis[p])%mod;
que.push(q);
}
}
}
return ;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int i, n, ans=0;
init();
scanf("%d", &n);
for(i=1; i<2*n-1; i++) //一条边变两条边
{
scanf("%d%d%d", &u[i], &v[i], &w[i]);
addedge(i, u[i]);
u[i+1] = v[i];
v[i+1] = u[i];
w[i+1] = w[i];
i++;
addedge(i, u[i]);
}
bfs(1);
int q, M, V, Y;
scanf("%d", &q);
while(q--)
{
scanf("%d %d %d", &M, &V, &Y);
printf("%d\n", (V-dis[M]+dis[Y]+mod)%mod); // 加上mod是为了防止出现负数
}
return 0;
}
/***************
Problem from :
Problem describe :
data:
****************/
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<stack>
#include<queue>
#include<ctime>
#include<cstring>
#include<vector>
#include<string>
#define ll __int64
#define inf 0x3f3f3f3f3f
using namespace std;
const int maxn = 100055;
const int mod = 707063423;
int u[2*maxn], v[2*maxn], w[2*maxn], first[maxn], next[2*maxn];
void init()
{
memset(first, -1, sizeof(first));
}
void addedge(int i, int u)//数组实现邻接表 (存边)
{
next[i] = first[u];
first[u] = i;
return ;
}
int dis[maxn]={0}, vis[maxn]={0}; //dis 距离 vis 是否用过
void bfs(int root) //求出各点到根节点的距离
{
queue<int>que;
dis[root] = 0;
vis[root]=1;
que.push(root);
while(!que.empty())
{
int p = que.front();
que.pop();
for(int i=first[p]; i!=-1; i=next[i])
{
int q = v[i];
if(!vis[q])
{
vis[q] = 1;
dis[q] = (w[i]+ dis[p])%mod;
que.push(q);
}
}
}
return ;
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int i, n, ans=0;
init();
scanf("%d", &n);
for(i=1; i<2*n-1; i++) //一条边变两条边
{
scanf("%d%d%d", &u[i], &v[i], &w[i]);
addedge(i, u[i]);
u[i+1] = v[i];
v[i+1] = u[i];
w[i+1] = w[i];
i++;
addedge(i, u[i]);
}
bfs(1);
int q, M, V, Y;
scanf("%d", &q);
while(q--)
{
scanf("%d %d %d", &M, &V, &Y);
printf("%d\n", (V-dis[M]+dis[Y]+mod)%mod); // 加上mod是为了防止出现负数
}
return 0;
}
相关文章推荐
- jboss-eap-6.3集成OracleDS和MySqlDS配置
- VmWare下CentOS 6.3 磁盘空间满处理操作
- Centos 6.3虚拟机及VMware tools的安装过程
- 6.3 cmath--数学函数
- 【poj1637】Sightseeing tour 混合图欧拉回路(最大流)
- centos6.3(64位) 安装apr
- 苹果电脑出现 .DS_Store文件,删除不掉怎么处理?
- mac系统下恢复和禁止.DS_store的生成
- Centos6.3 发送邮件
- centos6.5/6.3升级安装ImageMagick7.0.1-1
- CentOS 6.3图形界面自定义开启终端快捷键
- 第二个冲刺 6.3 6.8
- teg电话一面6.3
- RedHat Enterprise Linux 6.3 安装Oracle Database 11g
- Cannot change deployment state from ERROR to REDEPLOYING.ds
- 更改CentOS 6.3 yum源为国内 阿里云源
- CentOS6.3编译安装Memcached集群分布式缓存代理Magent-0.6出错汇总
- poj3621 Sightseeing Cows 【最大比例环 01分数规划】
- 单点登录之apacheds之springMVC下操作使用
- poj 1734 Sightseeing trip hdu 1599 find the mincost route 最小环 解题报告