一名苦逼的OIer,想成为ACMer

Iowa_Battleship

洛谷1608 路径统计

原题链接

模板题。
比起最短路计数(题解)这题多了边权,且计数的条件有所不同,需要去重边(取权值最小的),然后上\(dijkstra\)\(SPFA\)计数即可。
这里我是用的\(dijkstra\)

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int N = 2010;
const int M = N * N;
struct dd{
	int x, D;
	bool operator < (const dd &b) const
	{
		return D > b.D;
	}
};
int fi[N], di[M], ne[M], da[M], dis[N], cnt[N], a[N][N], l;
bool v[N];
priority_queue<dd>q;
inline int re()
{
	int x = 0;
	char c = getchar();
	bool p = 0;
	for (; c < '0' || c > '9'; c = getchar())
		p |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		x = x * 10 + c - '0';
	return p ? -x : x;
}
inline void add(int x, int y, int z)
{
	di[++l] = y;
	da[l] = z;
	ne[l] = fi[x];
	fi[x] = l;
	a[x][y] = z;
}
int main()
{
	int i, n, m, x, y, z;
	n = re();
	m = re();
	memset(a, 60, sizeof(a));
	for (i = 1; i <= m; i++)
	{
		x = re();
		y = re();
		z = re();
		if (z < a[x][y])
			add(x, y, z);
	}
	memset(dis, 60, sizeof(dis));
	cnt[1] = 1;
	dis[1] = 0;
	q.push((dd){1, 0});
	while (!q.empty())
	{
		x = q.top().x;
		q.pop();
		if (v[x])
			continue;
		v[x] = 1;
		for (i = fi[x]; i; i = ne[i])
		{
			if (dis[y = di[i]] > dis[x] + da[i])
			{
				dis[y] = dis[x] + da[i];
				cnt[y] = cnt[x];
				q.push((dd){y, dis[y]});
			}
			else
				if (!(dis[y] ^ (dis[x] + da[i])))
					cnt[y] += cnt[x];
		}
	}
	cnt[n] ? printf("%d %d", dis[n], cnt[n]) : printf("No answer");
	return 0;
}

posted on 2018-10-17 19:55  Iowa_Battleship  阅读(100)  评论(0编辑  收藏  举报

导航