求最短路条数之类的~~~

来源:互联网 发布:matlab2015 mac破解版 编辑:程序博客网 时间:2024/06/04 19:27

Flowery Trails

时间限制: 1 Sec  内存限制: 128 MB

题目描述

In order to attract more visitors, the manager of  a national park had the idea of planting flowers along both sides of the popular trails, which are the trails used by common people. Common people only go from the park entrance to its highest peak, where views are breathtaking, by a shortest path.So, he wants to know how many metres of flowers are needed to materialize his idea.

For instance, in the park whose map is depicted in the figure, common people make only one of the three following paths (which are the shortest paths from the entrance to the highest peak).
  •  At the entrance, some start in the rightmost trail to reach the point of interest 3 (after 100 metres), follow directly to point 7 (200 metres) and then pick the direct trail to the highest peak (620 metres).
  •  The others go to the left at the entrance and reach point 1 (after 580 metres). Then, they take one of the two trails that lead to point 4 (both have 90 metres). At point 4, they follow the direct trail to the highest peak (250 metres).
Notice that popular trails (i.e., the trails followed by common people) are highlighted in yellow. Since the sum of their lengths is 1930 metres, the extent of flowers needed to cover both sides of the popular trails is 3860 metres (3860 = 2 × 1930).

Given a description of the park, with its points of interest and (two-way) trails, the goal is to find out the extent of flowers needed to cover both sides of the popular trails. It is guaranteed that, for the given inputs, there is some path from the park entrance to the highest peak.

输入

The first line of the input has two integers: P and T. P is the number of points of interest  and T is the number of trails. Points are identified by integers, ranging from 0 to P − 1.The entrance point is 0 and the highest peak is point P − 1.

Each of the following T lines characterises a different trail. It contains three integers,p 1 , p 2 , and l, which indicate that the (two-way) trail links directly points p 1 and p 2 (not necessarily distinct) and has length l (in metres).
Integers in the same line are separated by a single space.


Constraints
2 ≤ P ≤ 10 000 Number of points.
1 ≤ T ≤ 250 000 Number of trails.
1 ≤ l ≤ 1 000 Length of a trail.

输出

The output has a single line with the extent of flowers (in metres) needed to cover both sides of the popular trails.

样例输入

10 150 1 5801 4 901 4 904 9 2504 2 5102 7 6007 3 2003 3 3803 0 1500 3 1007 8 5007 9 6209 6 5106 5 1455 9 160

样例输出

3860
#include<iostream>#include<cstdio>#include <cstdlib>#include <cstring>#include<queue>using namespace std;#define INF 0x3f3f3f3fconst int maxn = 250000+10;int n,m,cnt;int head[maxn*2],dis[maxn];bool vis[maxn];struct Node {    int to,cost;    Node(int a,int b) {        to = a;        cost = b;    }    bool operator<(const Node &a)const {        return cost > a.cost;    }};struct edg {    int to,cost,next;} edge[maxn];void addedge(int u,int v,int w) {    edge[cnt].to = v;    edge[cnt].cost = w;    edge[cnt].next = head[u];    head[u] = cnt ++;}void Dij(int st) {    memset(vis,0,sizeof(vis));    for(int i = 0; i <= n; i++)        dis[i] = INF;    dis[st] = 0;    priority_queue<Node>q;    q.push(Node(st,0));    int ans = 0;    while(!q.empty()) {        Node temp = q.top();        q.pop();        int v = temp.to;        if(vis[v])continue;        vis[v] = 1;        for(int i = head[v]; i != -1; i = edge[i].next) {            int to = edge[i].to;            int ds = edge[i].cost;            if(!vis[to]) {                if(dis[v] + ds < dis[to]) {                    dis[to] = dis[v] + ds;                    q.push(Node(to,dis[to]));                }                if(dis[v] + ds == dis[to])                    ans += 1;            }        }    }}int ok(int ed) {    memset(vis,0,sizeof(vis));    queue<int>q;    q.push(ed);    int ans = 0;    while(!q.empty()) {        int u = q.front();        q.pop();        for(int i = head[u]; i != -1; i = edge[i].next) {            int ds = edge[i].cost;            int v = edge[i].to;            if(ds + dis[v] == dis[u]) {                ans += ds;                if(!vis[v]) {                    vis[v] = 1;                    q.push(v);                }            }        }    }    return ans * 2;}// 或者将ok函数这样写,,,,感觉这样好一些吧int num[maxn],ans;//num里存的就是边数,edge_num = num[n-1],即为最短路条数(允许有相同的边int dfs(int cur) {    if(cur == 0)return 1;    for(int i = head[cur]; i != -1; i = edge[i].next) {        int to = edge[i].to;        int ds = edge[i].cost;        if(ds + dis[to] == dis[cur]) {            ans += ds;            if(vis[to])num[cur] += num[to];            else {                vis[to] = 1;                num[cur] += dfs(to);            }        }    }    return num[cur];}int main() {//    freopen("in.txt", "r", stdin);    scanf("%d%d",&n,&m);    int u,v,w;    memset(head,-1,sizeof(head));    cnt = 0;    for(int i = 0; i < m; i++) {        scanf("%d%d%d",&u,&v,&w);        addedge(u,v,w);        addedge(v,u,w);    }    Dij(0);    printf("%d\n",ok(n-1));    return 0;}

推荐两篇好的文章,写的很好,很容易懂:

http://www.tuicool.com/articles/FjqQVb6

http://www.cnblogs.com/XBWer/archive/2012/09/03/2669393.html


0 0
原创粉丝点击