zoj3408 Gao

wamaker posted @ 2010年10月08日 20:29 in 图论 with tags 最短路 , 2338 阅读

//这题Gao死我了!!!

Gao

题意:Gao这个函数可以用来算出有向图中从0到达某点的最短路径条数,用Gao算出0到所有点的最短路径条数了,现问有多少条路径经过点v。输出答案的后10位

解析:首先,这题有10000个点50000条边,对每点求最短路径是不可能的。观察最短路径以后我们可以知道,一个点v在一条最短路径里最多出现1次,要么出现在路径的末尾,要么出现在路径的中间,对于出现在末尾的最短路径数我们定义为cnt1[v],而我们在计算0点到所有点的最短路径过程中是可以记录cnt1[v]的:

 

if(dis[u]+w<dis[v])                  //u->v权值为w
{
	dis[v]=dis[u]+w;
	cnt1[v]=cnt1[u];
	tmp.v=v;tmp.dis=dis[v];
	que.push(tmp);
}
else if(dis[u]+w==dis[v])
{	cnt1[v]+=cnt1[u];
	cnt1[v]%=MOD;
}

 

至于v出现在最短路径的中间的情况,定义cnt2[v]表示最短路径中从v出发的路径条数,那么根据组合数学定义,v出现在最短路中间的最短路径条数=cnt1[v]*cnt2[v]。cnt2[v]可以用Calc(v)进行递归计算(貌似也可以叫树形DP,好像还可以拓扑排序以后之间DP),假如有t1,t2,t3...tn与v邻接且v->t1,v->t2,v->t3...,v->tn都在最短路径上,那么同样根据组合数学加法原理有cnt2[v]=cnt2[t1]+cnt2[t2]+...cnt[tn]。因为t1,t2,tn都在最短路径上,所以cnt2[v]+=n;

最终ans[v]=cnt1[v]+cnt1[v]*cnt2[v]。

这题比较阴险的地方在于答案要保留后10位,而进行乘法的过程中会超long long(事实证明,在计算cnt1的过程中也会超long long,在这地方无限WA T_T)要用到布斯(Booth)乘法:

 

long long Mul(long long lhs,long long rhs)
{
	long long lhs2=lhs%100000;
	long long rhs2=rhs%100000;
	return ((lhs/100000*rhs2+rhs/100000*lhs2)*100000+lhs2*rhs2)%MOD;
}

最后贴一个完整的代码:

 

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

const long long MOD=10000000000LL;

struct Edge
{
	int v,next,w;
}buf[50010];

struct Node
{
	int v,dis;
};

const int INF=1<<30;

bool operator<(Node a,Node b)
{
	return a.dis>b.dis;
}

int head[10010];
int dis[10010];
long long cnt[10010];
bool flag[10010];
int N,M,nE,Q;

void addEdge(int u,int v,int w)
{
	buf[nE].v=v;buf[nE].w=w;buf[nE].next=head[u];head[u]=nE++;
}

long long Mul(long long lhs,long long rhs)
{
	long long lhs2=lhs%100000;
	long long rhs2=rhs%100000;
	return ((lhs/100000*rhs2+rhs/100000*lhs2)*100000+lhs2*rhs2)%MOD;
}

long long add(long long a,long long b)
{
	a+=b;
	if(a>=MOD)
		a-=MOD;
	return a;
}

void Dijkstra(int src)
{
	Node tmp,cur;
	tmp.v=src;tmp.dis=0;
	for(int i=0;i<=N;i++)
		dis[i]=INF;
	dis[src]=0;

	memset(flag,false,sizeof(flag));
	memset(cnt,0,sizeof(cnt));
	priority_queue<Node> que;
	cnt[src]=1;
	que.push(tmp);
	while(!que.empty())
	{
		cur=que.top();que.pop();
		int u=cur.v,d=cur.dis;
		if(flag[u])continue;
		flag[u]=true;
		for(int i=head[u];i!=-1;i=buf[i].next)
		{
			int v=buf[i].v,w=buf[i].w;
			if(d+w<dis[v])
			{
				dis[v]=d+w;
				cnt[v]=cnt[u];
				tmp.v=v;tmp.dis=dis[v];
				que.push(tmp);
			}
			else if(d+w==dis[v])
			{	cnt[v]+=cnt[u];
				cnt[v]%=MOD;
			}
		}
	}
}

long long rec[10010];

long long Gao(int s)
{
	long long c=0;
	if(rec[s]!=-1)return rec[s];
	for(int i=head[s];i!=-1;i=buf[i].next)
	{
		int v=buf[i].v;
		if(dis[s]+buf[i].w==dis[v]&&s!=v)
		{
			c++;
			if(rec[v]==-1)
				rec[v]=Gao(v);
			c=add(c,rec[v]);
		}
	}
	return c%MOD;
}

int main()
{
	//freopen("data","r",stdin);
	//freopen("mout","w",stdout);
	while(scanf("%d%d%d",&N,&M,&Q)!=EOF)
	{
		memset(head,0xff,sizeof(head));
		memset(rec,0xff,sizeof(rec));
		nE=0;
		while(M--)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			addEdge(u,v,w);
		}
		Dijkstra(0);
		int q;
		while(Q--)
		{
			scanf("%d",&q);
			if(cnt[q]<0)while(1);
			long long ans=Mul(cnt[q],Gao(q)+1);
			ans=ans%MOD;
			printf("%010lld\n",ans);
		}
		//putchar(10);
	}
	return 0;
}
  • 无匹配
Avatar_small
SSO Rajasthan 说:
2022年8月02日 14:55

SSO stands for Single Sign On and SSO Rajasthan for any citizen to create their digital identity. According to their official website, more than 18672702 identities have been create from their official website. SSO Rajasthan There are different benefits of being a part of this online platform out of which the primary one is to be under the Jan Aadhaar scheme and benefits. Along with that you can also get benefits from schemes such as Bhamashah but before you do, you have to create your account and in the below guide we have explained the exact steps you need to follow, and if any queries, you may contact SSO customer care.

Avatar_small
Tenda Login 说:
2023年2月03日 13:45

The popular brand Tenda supplies various wireless routers which well known for easy configuration of the WiFi network. Once a new Router bought, one should make it ready to configure such that their LAN connection does move to WiFi to used by multiple devices. Tenda Login Tenda is the most preferred brand in customer premises wireless equipment due to its good range and stable connection. This difference in Bandwidth does allow the WiFi Router to handle better internet for any kind of bandwidth requested.

Avatar_small
mi tv samsung no se 说:
2023年7月31日 00:04

Es posible que el Wi-Fi no funcione en su televisor Samsung pero esté funcionando en otros dispositivos, o puede estar conectado al punto de acceso de su teléfono y acceder a Internet de esa manera, mi tv samsung no se conecta a wifi pero se niega a conectarse a Internet a través de su enrutador. Esta guía simple lo ayudará a volver a conectar su televisor Samsung en línea si no se conecta a Internet pero otros dispositivos pueden hacerlo o si Wi-Fi no funciona en absoluto.

Avatar_small
KVS 6th Class Split 说:
2023年9月02日 18:16

Kendriya Vidyalaya (KVS) 6th Class Students Regular Reading new Syllabus for Half Yearly, Final Exam Easy to Pass Curriculum for the Academic Year 2024, KVS 6th class Students Download your KVS 6th Split Up Syllabus 2024 Online Pdf Format.This year KVS has Announced 6th Class Split Up Syllabus for for the subjects English, Hindi, Mathematics, Sanskrit, Science, SST, German etc. have KVS 6th Class Split up Syllabus 2024 a look at the Exam Pattern and Syllabus for KVS 8th Class Curriculum for the Academic Year 2024,Kendriya Vidyalaya (KVS) 6th Class Curriculum Syllabus & Exam Pattern Download Regular Redding Best Performs in for Home Work & Final Exam Curriculum for the Academic Year 2024.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter