程序设计竞赛(同步赛) C题 City Supplies 最短路 迪杰斯特拉+堆优化"/>
东华大学2020年程序设计竞赛(同步赛) C题 City Supplies 最短路 迪杰斯特拉+堆优化
链接:
来源:牛客网
题目描述
YZ is the king of the kingdom. There are n cities in his kingdom. To celebrate the 50th anniversary of the founding of his country, YZ decided to distribute supplies as a reward to all the cities.
We all know that the further the city is from the capital (always at 1), the more it will cost to transport. We define K as the shortest distance between a city and the capital, and ignore the difference in distance between different cities(all is 1 unit). Cost from capital to the city is 2K2^K2K.
Now YZ gives you the map of his kingdom, and asks you if you can calculate the total cost.
(We guarantee that it is a connected graph.)
输入描述:
The first line contains two integers n and m (1≤n≤106,n−1≤m≤min(2∗106,(n−1)∗n/2))(1 \le n \le 10^6,n-1 \le m \le min(2*10^6,(n-1)*n/2))(1≤n≤106,n−1≤m≤min(2∗106,(n−1)∗n/2)), which means there are n cities and m roads.
The next m lines contains two integers u and v, denoting there is a road connecting city u and city v.
输出描述:
Print the only line containing a single integer. It should be equal to the total cost mod 1e9+7.
示例1
输入
复制
3 2
1 2
2 3
输出
复制
6
示例2
输入
复制
7 6
1 2
1 3
1 4
3 5
3 6
4 7
输出
复制
18
题意 : 给定一张无向图,源点为 1 1 1, 两点之间的边权为 2 K 2^K 2K, 源点到其他点求边权和mod
1e9+7
如图
注意点 :
- 要预处理出所有 2 k 2^k 2k(一开始直接暴力快速幂 T L E TLE TLE了)
#define debug
#ifdef debug
#include <time.h>
#include </home/majiao/mb.h>
#endif#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>#define MAXN ((int)1e6+7)
#define ll long long
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)using namespace std;#define show(x...) \do { \cout << "\033[31;1m " << #x << " -> "; \err(x); \} while (0)void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }namespace FastIO{char print_f[105];void read() {}void print() { putchar('\n'); }template <typename T, typename... T2>inline void read(T &x, T2 &... oth) {x = 0;char ch = getchar();ll f = 1;while (!isdigit(ch)) {if (ch == '-') f *= -1; ch = getchar();}while (isdigit(ch)) {x = x * 10 + ch - 48;ch = getchar();}x *= f;read(oth...);}template <typename T, typename... T2>inline void print(T x, T2... oth) {ll p3=-1;if(x<0) putchar('-'), x=-x;do{print_f[++p3] = x%10 + 48;} while(x/=10);while(p3>=0) putchar(print_f[p3--]);putchar(' ');print(oth...);}
} // namespace FastIO
using FastIO::print;
using FastIO::read;int n, m, Q, K;struct Edge {int to, val;
} ;typedef pair<int, int> pii;
vector<Edge> G[MAXN];int d[MAXN];
void dij() {int s = 1;memset(d, INF, sizeof(d));priority_queue<pii> q;q.push({-d[s], s}); d[s] = 0;while(!q.empty()) {auto now = q.top(); q.pop();int u = now.second;for(auto ed : G[u]) {int v = ed.to, td = ed.val;if(d[v] > d[u]+td) {d[v] = d[u] + td;q.push({-d[v], v});}}}
}
ll len[MAXN] = { 0 };signed main() {
#ifdef debugfreopen("test", "r", stdin);clock_t stime = clock();
#endifscanf("%d %d ", &n, &m);while(m--) {int u, v;scanf("%d %d ", &u, &v);G[u].push_back({v, 1}), G[v].push_back({u, 1});}dij();int tmax = 0;for(int i=1; i<=n; i++) tmax = max(tmax, d[i]);len[0] = 1;int mod = 1e9+7;for(int i=1; i<=tmax; i++) {len[i] = len[i-1] << 1;if(len[i] >= mod) //不优化这里不给过 ???len[i] %= mod;}ll ans = 0;for(int i=2; i<=n; i++) {ans += len[d[i]]; if(ans >= mod) ans %= mod; 不优化这里不给过 ???}printf("%lld\n", ans);#ifdef debugclock_t etime = clock();printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif return 0;
}
更多推荐
东华大学2020年程序设计竞赛(同步赛) C题 City Supplies 最短路 迪杰斯特拉+堆优化
发布评论