秋实大哥带我飞

Sample input and output

Sample Input Sample Output
4 4
1 2 1
1 3 1
2 4 1
3 4 1
2
4 4
1 2 1
1 3 1
2 4 1
3 4 0
-1

Source

2015 UESTC Training for Graph Theory

/*******************************************************************************
*** problem ID  : UESTC_1147.cpp
*** create time : Wed Nov 18 14:22:41 2015
*** author name : nndxy
*** author blog : http://blog.csdn.net/jhgkjhg_ugtdk77
*** author motto: never loose enthusiasm for life, life is to keep on fighting!
*******************************************************************************/

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <cctype>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

#define exit() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxn = 2000 + 5;
const int maxm = 2000 + 5;
const int INF = 0x7f7f7f7f;
const ll mod = 1e9 + 9;

struct P{
int to, dis;
P() {}
P(int _to, int _dis) : to(_to), dis(_dis) {}
friend bool operator<(P a, P b) { return a.dis > b.dis; }
};

struct Edge{
int u, v, dis, next;
};

int n, m;
int a, b, c, num;
Edge edge[maxm * 2];
ll sum1[maxn], sum2[maxn];
int dist1[maxn], dist2[maxn];

void addEdge(int u, int v, int dis){
edge[num].u = u;
edge[num].v = v;
edge[num].dis = dis;
}

void input(){
scanf("%d%d", &n, &m);
num = 0;
for(int i = 0; i < m; i++){
scanf("%d%d%d", &a, &b, &c);
}
}

void dijkstra(int dist[], ll sumt[], int s){
fill(dist + 1, dist + 1 + n, INF);
fill(sumt + 1, sumt + 1 + n, 0);
priority_queue <P> q;
while(!q.empty()) q.pop();

dist[s] = 0; sumt[s] = 1;
q.push(P(s, 0));

while(!q.empty()){
P p = q.top(); q.pop();
int u = p.to;
if(p.dis > dist[u]) continue;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(dist[v] > dist[u] + edge[i].dis){
sumt[v] = sumt[u] % mod;
dist[v] = dist[u] + edge[i].dis;
q.push(P(v, dist[v]));
}
else if(dist[v] == dist[u] + edge[i].dis){
sumt[v] = (sumt[v] + sumt[u]) % mod;
}
}
}
}

bool check(){
for(int i = 0; i < num; i++){
int u = edge[i].u;
int v = edge[i].v;
if(edge[i].dis == 0 && dist1[u] + dist2[v] == dist1[n]){
return true;
}
}
return false;
}

void solve(){
dijkstra(dist1, sum1, 1);
dijkstra(dist2, sum2, n);
if(check()) printf("-1\n");
else printf("%lld\n", sum1[n] % mod);
}

int main(){
input();
solve();
exit();
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：CDOJ_1147 (最短路条数) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)