P2384 shortest shortest

Topic background

Dog rotten brother do most short-circuit, suddenly a witty exam Bosh, Bosh did not expect the test Bosh live ... can you help solve it?

He will give you \ (100000000000000000000000000000000000 \) % \ (10 \) gold w

Title Description

Given n points weighted directed graph, find the path from 1 to n of the product of the smallest edge weights of simple paths.

Input Format

The first line reads two integers n, m, m represents a total of n points edges. Next m lines of three positive integers x, y, z, represents the point x to point y as there is a right side edge of z.

Output Format

The output includes only one line, denoted by the path of seeking the right side of the plot, because the answer may be large, so the dog brother graciously let you output it die \ (9987 \) the remainder can be.

Of course, the number is a nonsense w

// Xie fyszzhouzj correction w

For 20% of the data, \ (n-<= 10 \) .

To 100% of the data, \ (n-<= 1000, m <= 1000000 \) . The right side does not exceed \ (10000 \) .

Sample input and output

Input # 1

3 3
1 2 3 
2 3 3 
1 3 10

Output # 1

9

Description / Tips

Take a good look to write yo w

answer

Consider how the issue will be transformed meaning of the questions is to make the product of the largest, most can only deal with short-circuit and the biggest plus.

Log operation to take as Log monotonically increasing, and the multiplication can be converted into Log addition operation.

Thus, on the right side of the path is calculated and taken Log shortest run.

pre [i] records point i precursor, p_val point i and precursors attached edge weights [i] records.

The answer can be recorded.

code:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
const int N = 1e3 + 5, M = 1e6 + 5, mod = 9987;
int read() {
    int x = 0, f = 1; char ch = getchar();
    while(! isdigit(ch)) f = (ch=='-')?-1:1, ch = getchar();
    while(isdigit(ch)) x = (x<<3)+(x<<1)+(ch^48), ch = getchar();
    return x * f;
}
int n, m, ans, cnt, head[N], vis[N], pre[N], p_val[N];
struct edge { int to, nxt, val; } e[M];
double dis[N];
void add(int from, int to, int val) {
    e[++ cnt].to = to;
    e[cnt].val = val;
    e[cnt].nxt = head[from];
    head[from] = cnt;
}
void spfa(int s) {
    queue <int> q; q.push(s);
    for(int i = 1;i <= n;i ++) dis[i] = 1e9 + 7;
    dis[s] = 0; vis[s] = 1;
    while(! q.empty()) {
        int tp = q.front(); q.pop();
        vis[tp] = 0;
        for(int i = head[tp]; i ;i = e[i].nxt) {
            int to = e[i].to;
            if(vis[to]) continue;
            double dist = log(e[i].val);
            if(dis[to] > dis[tp] + dist) {
                dis[to] = dis[tp] + dist;
                pre[to] = tp; p_val[to] = e[i].val;
                if(! vis[to]) q.push(to), vis[to] = 1;
            }
        }
    }
    ans = 1;
    int t = n;
    while(t != 1) {
        (ans *= p_val[t]) %= mod;
        t = pre[t];
    }
}
int main() {
    n = read(); m = read();
    for(int i = 1, x, y, z;i <= m;i ++) {
        x = read(); y = read(); z = read();
        add(x, y, z); add(y, x, z);
    }
    spfa(1);
    printf("%d\n", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Paranoid-LS/p/11329039.html