题目传送门
这题我要先吐槽一波:更改数据类型竟然还影响时间复杂度,本来所有整型数据都换成了long long ,结果超时,把其中部分整型数据更改为
i
n
t
int
int,
W
C
WC
WC竟然过了,吐了。。。
言归正传:简述一下题意,给出一张图,图上有n个顶点,m条变,找出所有点到1这个顶点的最短距离,并计算价值。总结来说主要就是求单源最短路。对于边权都相等且为1的这道题来说,用BFS求最短路也是可以,迪杰斯特拉的话(边权大于等于0)都能求。所以也就产生了下面所列的两种代码,另外注意最好是加上读入挂,经过多次死亡试探,这题确实卡时间卡的太狠了。能优化尽量优化就ok。对了对了,求价值的时候还需要用到一个快速幂。别的差不多没了,接下来附上两种方法的代码
1、Dijkstra算法求解代码:
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include<cstdio>
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
typedef pair<ll, ll> PII;
const int N = 4e6 + 10;
#define I_int ll
#define inf 0x3f3f3f3f
int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N];
bool st[N];
ll qpow(ll x, ll y)
{
ll res = 1;
while (y)
{
if (y & 1) res = res * x % mod;
x = x * x % mod;
y>>=1;
}
return res;
}
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra()
{
memset(dist,inf,sizeof dist);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({ 0, 1 });
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second, distance = t.first;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[ver] + w[i])
{
dist[j] = dist[ver] + w[i];
heap.push({ dist[j], j });
}
}
}
}
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
char F[200];
inline void out(I_int x) {
if (x == 0) return (void) (putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
//cout<<" ";
}
int main()
{
n = read(), m = read();
memset(h,-1,sizeof h);
for(int i=1;i<=m;i++)
{
int a, b;
a = read(), b = read();
add(a, b, 1);
add(b, a, 1);
}
dijkstra();
ll ans=0;
for(int i=2;i<=n;i++)
ans=ans+qpow(2,dist[i])%mod,ans%=mod;
out(ans%mod);
return 0;
}
2、BFS算法求解:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
int n, m;
const int N = 2000010;
vector<int> v[N];
const int mod = 1e9 + 7;
#define I_int ll
#define inf 0x3f3f3f3f
ll qpow(ll x, ll y)
{
ll res = 1;
while(y)
{
if (y & 1) res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
bool vis[N];
int d[N];
void bfs(int x)
{
memset(vis, false, sizeof vis);
queue<int> q;
q.push(x);
vis[x] = true;
while (q.size())
{
int t = q.front();q.pop();
for (auto it : v[t])
{
if (!vis[it])
{
vis[it] = true;
d[it] = d[t] + 1;
q.push(it);
}
}
}
}
inline ll read()
{
ll x = 0, f = 1;char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }
return x * f;
}
char F[200];
inline void out(I_int x) {
if (x == 0) return (void)(putchar('0'));
I_int tmp = x > 0 ? x : -x;
if (x < 0) putchar('-');
int cnt = 0;
while (tmp > 0) {
F[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while (cnt > 0) putchar(F[--cnt]);
//cout<<" ";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
n = read(), m = read();
for (int i = 1;i <= m;i++)
{
int x, y;
x = read(), y = read();
v[x].push_back(y);
v[y].push_back(x);
}
bfs(1);
d[1] = 0;
ll ans = 0;
/*for (int i = 1;i <= n;i++)
{
cout << d[i] << " ";
}*/
for (int i = 2;i <= n;i++)
{
ans = (ans + qpow(2, d[i]) % mod) % mod;
}
out(ans);
return 0;
}
其实我感觉BFS这个版本更好理解一点,就是遍历所有点,遇到新的顶点就更新其距离即可,也许只是个人偏见。说实话,因为第一种方法我只是会套个模板,hh,并没有理解的太深,太菜太菜,接下来要再学学啦,