UVA10917 A walk trough the Forest (最短路,dp)
求出家到其他点的最短路径,题目的条件变成了u->v不是回头路等价于d[u]>d[v]。
然后根据这个条件建DAG图,跑dp统计方案数,dp[u] = sum(dp[v])。
#include<bits/stdc++.h>
using namespace std; const int maxn = , maxm = ;
struct Edge
{
int v,w,nxt;
}; #define PB push_back
vector<Edge> edges;
vector<int> G[maxn];
int head[maxn];
int d[maxn]; void addEdge(int u,int v,int w)
{
edges.PB({v,w,head[u]});
head[u] = edges.size()-;
} void init()
{
memset(head,-,sizeof(head));
edges.clear();
} typedef pair<int,int> Node;
#define fi first
#define se second
void dijkstra(int s = )
{
memset(d,0x3f,sizeof(d));
priority_queue<Node,vector<Node>,greater<Node> > q;
q.push(Node(d[s] = ,s));
while(q.size()){
Node x = q.top(); q.pop();
int u = x.se;
if(x.fi != d[u]) continue;
for(int i = head[u]; ~i ; i = edges[i].nxt){
Edge &e = edges[i];
if(d[e.v] > d[u]+e.w){
d[e.v] = d[u]+e.w;
q.push(Node(d[e.v],e.v));
}
}
}
} int dp[maxn];
int dfs(int u)
{
int &ans = dp[u];
if(~ans) return ans;
if(u == ) return ans = ;
ans = ;
for(int i = ; i < (int)G[u].size(); i++){
ans += dfs(G[u][i]);
}
return ans;
} void rebuild(int n)
{
for(int i = ; i < n; i++) G[i].clear();
for(int u = ; u < n; u++){
for(int i = head[u]; ~i; i = edges[i].nxt){
int v = edges[i].v;
if(d[v] < d[u]) G[u].PB(v);
}
}
} int main()
{
//freopen("in.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m),n){
init();
while(m--){
int u,v,w; scanf("%d%d%d",&u,&v,&w); u--;v--;
addEdge(u,v,w); addEdge(v,u,w);
}
dijkstra();
rebuild(n);
memset(dp,-,sizeof(dp));
printf("%d\n",dfs());
}
return ;
}
UVA10917 A walk trough the Forest (最短路,dp)的更多相关文章
- A Walk Through the Forest (最短路+记忆化搜索)
Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...
- 【uva10917】Walk Through the Forest (最短路)
题目: gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共 ...
- UVA-10917 Walk Through the Forest (dijkstra+DP)
题目大意:n个点,m条边的无向图.一个人从起点到终点按照下面的走法:从A走向B当A到终点的最小距离比B到终点的最小距离大时.问从起点到终点有多少路径方案. 题目分析:先用dijkstra预处理出终点到 ...
- UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)
题目给一张有向图,问从起点1到终点2沿着合法的路走有种走法,合法的路指从u到v的路,v到终点的距离严格小于u到终点的距离. 先SPFA预处理出所有合法的路,然后这些路肯定形成一个DAG,然后DP一下就 ...
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
- HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1142 A Walk Through the Forest(最短路+dfs搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
随机推荐
- sql语句之查询操作
语法顺序: select distinct 字段1,字段2,字段3 from 库.表 where 条件 group by 分组条件 having 过滤 # 执行顺序的话,到这步会返回运行select语 ...
- CCF 201512-4 送货 (并查集+DFS,欧拉路)
问题描述 为了增加公司收入,F公司新开设了物流业务.由于F公司在业界的良好口碑,物流业务一开通即受到了消费者的欢迎,物流业务马上遍及了城市的每条街道.然而,F公司现在只安排了小明一个人负责所有街道的服 ...
- cocos2dx 新手引导
static CCClippingNode* create(); //使用一个节点作为模版创建裁剪节点 static CCClippingNode* create(CCNode *pStencil); ...
- ASP.NET中在后台用C#,往前台插入HTML代码
//你的div加ID号,然后写上runat="server",变成服务器端控件,然后后台可以直接用ID号.innerhtml="html内容",这样就可以了 & ...
- E20180715-hm
grapefruit n. 葡萄柚,西柚; 葡萄柚树;
- DMOJ IOI '17 P3 - Toy Train【拓扑排序】
传送:https://dmoj.ca/problem/ioi17p3 参考:https://blog.csdn.net/qq_27327327/article/details/80711824 妙啊- ...
- 洛谷P3306 [SDOI2013]随机数生成器(BSGS)
传送门 感觉我BSGS都白学了……数学渣渣好像没有一道数学题能自己想出来…… 要求$X_{i+1}=aX_i+b\ (mod \ \ p)$ 左右同时加上$\frac{b}{a-1}$,把它变成等比数 ...
- linux ping
ping [ -d] [ -D ] [ -n ] [ -q ] [ -r] [ -v] [ -R ] [ -a addr_family ] [ -c Count ] [ -w timeout ...
- java操作redis实现和mysql数据库的交互
连接地址http://blog.csdn.net/kingcat666/article/details/77936970
- UItableView动态行高 用这两句实现(可以自己计算数据来实现,一般在model中计算)
// 动态行高 self.tableView.rowHeight = UITableViewAutomaticDimension; // 预估行高 self.tableView.estimatedRo ...