您的位置:首页 > 其它

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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: