abc_d
  • 板块学术版
  • 楼主迟暮天复明心華
  • 当前回复5
  • 已保存回复5
  • 发布时间2021/7/24 22:06
  • 上次更新2023/11/4 13:24:42
查看原帖
abc_d
222865
迟暮天复明心華楼主2021/7/24 22:06

从来T一个点,莫名惊愕。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<map>
#include<math.h>
#include<time.h>
#include<iostream>
#include<vector>
#include<stdlib.h>
#define int ll
#define MOD 1000000007
#define reg register
#define ri reg int
#define rep(i, x, y) for(ri i = x; i < y; ++i)
#define nrep(i, x, y) for(ri i = x; i >= y; --i)
#define DEBUG 1
#define ll long long
#define il inline
#define swap(a, b) ((a) ^= (b) ^= (a) ^= (b))
#define max(i, j) ((i) > (j) ? (i) : (j))
#define min(i, j) ((i) < (j) ? (i) : (j))
template <class T> 
inline T gcd(T a, T b) {
	return b == 0 ? a : gcd(b, a % b);
}
template <class T> 
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
namespace IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  ~IO() {
    fwrite(pbuf, 1, pp - pbuf, stdout);
  }
#endif
  inline char gc() {
#if DEBUG
    return getchar();
#endif
    if(p1 == p2)
      p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void read(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for(; !isdigit(ch); ch = gc())
      if(ch == '-') sign = 1;
    for(; isdigit(ch); ch = gc())
      x = x * 10 + (ch - '0');
    if(ch == '.')
      for(ch = gc(); isdigit(ch); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if(sign) x = -x;
  }
  inline void read(char *s) {
    register char ch = gc();
    for(; blank(ch); ch = gc());
    for(; !blank(ch); ch = gc())
      *s++ = ch;
    *s = 0;
  }
  inline void read(char &c) {
    for(c = gc(); blank(c); c = gc());
  }
  inline void push(const char &c) {
#if DEBUG
    putchar(c);
#else
    if(pp - pbuf == MAXSIZE) {
      fwrite(pbuf, 1, MAXSIZE, stdout);
      pp = pbuf;
    }
    *pp++ = c;
#endif
  }
  template <class T>
  inline void print(T x) {
    if(x < 0) {
      x = -x;
      push('-');
    }
    static T sta[35];
    T top = 0;
    do {
      sta[top++] = x % 10;
      x /= 10;
    } while(x);
    while(top)
      push(sta[--top] + '0');
  }
  template <class T>
  inline void print(T x, char lastChar) {
    print(x);
    push(lastChar);
  }
}
using namespace IO;
struct edge {
    int to, dis, nxt;
}e[10000010];
struct node
{
    int dis;
    int pos;
    bool operator <( const node &x )const
    {
        return x.dis < dis;
    }
};
int head[4000010], dis[4000010], cnt, n, m, s, vis[4000100];

int f[4000010];
void add_edge(int u, int v) {
    ++cnt;
    e[cnt].dis = 1;
    e[cnt].to = v;
    e[cnt].nxt = head[u];
    head[u] = cnt;
}
std::priority_queue<node> q;
void dijkstra() {
	int x, d, i, y;
    dis[s] = 0;
    q.push((node){0, s});
    while(!q.empty()) {
        node tmp = q.top();
        q.pop();
        x = tmp.pos;
		d = tmp.dis;
        if(vis[x]) continue;
        vis[x] = 1;
        for(i = head[x]; i; i = e[i].nxt) {
            y = e[i].to;
            if(dis[y] > dis[x] + e[i].dis) {
                dis[y] = dis[x] + e[i].dis;
                if(!vis[y]) q.push( ( node ){dis[y], y} );
            }
        }
    }
}
void dfs(int u, int fa) {
	//f[u] = 1;
	if(f[u]) return;
	if(u == n) return;
	for(int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].to;
		if(v == fa) continue;
		if(dis[v] == dis[u] + 1) dfs(v, u), f[u] += f[v], f[u] %= MOD;
		//printf("%d %d %d %d %d\n", fa, u, v, f[u], f[v]);
	}
	
}
	
	
signed main() {
	int i, u, v;
	read(n), read(m);
    s = 0;
    memset(dis, 0x3f, sizeof(dis));
    
    for(i = 1; i <= m; ++i) {
    	read(u), read(v);
        add_edge(u, v), add_edge(v, u);
    }
    add_edge(0, 1);
    add_edge(1, 0);
    dijkstra();
    memset(vis, 0, sizeof(vis));
    f[n] = 1;
    dfs(1, 0);
    
    print(f[1], '\n');
    //for(i = 1; i <= n; ++i) printf("%d ", dis[i]);
    return 0;
}

分数应该掉的很惨,谁来调一下

2021/7/24 22:06
加载中...