求最短路条数之类的~~~
来源:互联网 发布: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).
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.
输出
样例输入
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
- 求最短路条数之类的~~~
- 优先队列优化的求最短路和次短路条数以及长度的模板
- 最短路条数
- eoj1818 dijkstra求最短路及其条数
- PAT 1003. Emergency (25) (求两点间最短路的条数)
- HDU1688 Sightseeing(SPFA 求最短路与次短路的路径条数)可用作模板
- PAT L3-011. 直捣黄龙(求最短路条数,三个关键词优先,最短路路径)
- Dijkstra求最短路的条数,并输出最短路径和最短路经过的点的最大和
- CDOJ_1147 (最短路条数)
- 最短路条数模板
- 最短路条数计数
- POJ_3463_Sightseeing(最短路/次短路条数)
- 最短路和次短路条数
- HDU 1688 Sightseeing 求最短路和次短路条数之和
- poj3463Sightseeing(最短路条数+次短路条数)
- zoj 2760(边不相交最短路的条数)
- hdu 1142 A Walk Through the Forest【Dijkstra+dfs求最短路条数】
- Codeforces 567E President and Roads (用Dijkstra求最短路条数 + 强连通桥)
- 新手 Python-机器学习 四部曲资源汇总
- PHP中怎么同时具有POST和GET的数据呢?
- 搜索算法(Search)
- 178_自定义滚动View
- ComboBox自动加载值
- 求最短路条数之类的~~~
- java之查询sql server数据库数据
- 179_Fiddler
- JAVA 泛型 - Class<T>
- Mysql关闭与启动
- 180_自定义ActionBar
- sql语句之join和where
- Oracle存储过程和自定义函数
- 用SIFT特征和RANSAC算法进行两幅图片的匹配