跳到正文

剪贴板

剪贴板 3r03knvi

原文
剪贴板信息

剪贴板信息

收录
更新

Day5 C

C++
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

const int maxn=100010;
const int maxm=200010;
const int mo=1000000007;

#define inc(a,b) {a+=b;if (a>=mo) a-=mo;}

int n,m,en,dist[maxn],a[maxn],f[maxn],g[maxn];

bool inq[maxn];

queue<int> q;

struct edge
{
	int e,d;
	edge *next;
}*v[maxn],ed[maxm];

void add_edge(int s,int e,int d)
{
	en++;
	ed[en].next=v[s];v[s]=ed+en;v[s]->e=e;v[s]->d=d;
}

bool cmp(int a,int b)
{
	return dist[a]<dist[b];
}

int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
	{
		int s,e,d;
		scanf("%d%d%d",&s,&e,&d);
		add_edge(s,e,d);
	}
	memset(dist,0x3f,sizeof(dist));
	dist[1]=0;
	q.push(1);
	inq[1]=true;
	while (q.size())
	{
		int now=q.front();
		q.pop();
		inq[now]=false;
		for (edge *e=v[now];e;e=e->next)
		{
			int newd=dist[now]+e->d;
			if (newd<dist[e->e])
			{
				dist[e->e]=newd;
				if (!inq[e->e]) q.push(e->e),inq[e->e]=true;
			}
		}
	}
	for (int i=1;i<=n;i++)
		a[i]=i;
	sort(a+1,a+n+1,cmp);
	f[1]=1;
	g[1]=0;
	int ans1=0,ans2=0;
	for (int i=1;i<=n;i++)
	{
		int now=a[i];
		if (dist[now]==dist[0]) break;
		inc(ans1,dist[now]);
		inc(ans2,g[now]);
		for (edge *e=v[now];e;e=e->next)
			if (dist[e->e]==dist[now]+e->d)
			{
				inc(f[e->e],f[now]);
				inc(g[e->e],f[now]);
				inc(g[e->e],g[now]);
			}
	}
	printf("%d %d\n",ans1,ans2);

	return 0;
}