码迷,mamicode.com
首页 > 其他好文 > 详细

UVA10917 Walk Through the Forest

时间:2018-07-22 00:10:13      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:广搜   记录   ora   can   -o   bsp   ems   fine   mem   

题目描述

PDF

技术分享图片

输入输出格式

输入格式:

 

技术分享图片

 

输出格式:

 

技术分享图片

 

输入输出样例

输入样例#1: 
5 6
1 3 2
1 4 2
3 4 3
1 5 12
4 2 34
5 2 24
7 8
1 3 1
1 4 1
3 7 1
7 4 1
7 5 1
6 7 1
5 2 1
6 2 1
0
输出样例#1: 
2
4

 

 

Solution:

  常规()题,考试的时候细节写挂。

  首先以$2$为源点处理出到其他点的最短路,然后遍历一遍图,标记那些不满足$dis[u]>dis[v]$的边,最后只要从$1$广搜一下处理出答案就好了,广搜时记录一下所到边的变化值,用变化值去更新答案。

代码:

 

 1 #include<bits/stdc++.h>
 2 #define il inline
 3 #define ll long long
 4 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
 5 #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
 6 using namespace std;
 7 const int N=100005;
 8 int n,m,to[N],net[N],w[N],dis[N],h[N],cnt;
 9 int ans[N],deta[N];
10 bool vis[N],ct[N];
11 
12 il int gi(){
13     int a=0;char x=getchar();bool f=0;
14     while((x<0||x>9)&&x!=-)x=getchar();
15     if(x==-)x=getchar(),f=1;
16     while(x>=0&&x<=9)a=(a<<3)+(a<<1)+x-48,x=getchar();
17     return f?-a:a;
18 }
19 
20 il void add(int u,int v,int c){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt,w[cnt]=c;}
21 
22 queue<int>q;
23 
24 il void pre(){
25     memset(dis,0x3f,sizeof(dis));
26     memset(ct,0,sizeof(ct));
27     dis[2]=0;q.push(2);
28     while(!q.empty()){
29         int u=q.front();q.pop();vis[u]=0;
30         for(int i=h[u];i;i=net[i])
31             if(dis[to[i]]>dis[u]+w[i]){
32                 dis[to[i]]=dis[u]+w[i];
33                 if(!vis[to[i]])vis[to[i]]=1,q.push(to[i]);
34             }
35     }
36     For(u,1,n)
37         for(int i=h[u];i;i=net[i])
38             if(dis[u]<=dis[to[i]]) ct[i]=1;
39 }
40 
41 il void solve(){
42     memset(ans,0,sizeof(ans));
43     ans[1]=1;deta[1]=1;
44     q.push(1);
45     while(!q.empty()){
46         int u=q.front();vis[u]=0;q.pop();
47         for(int i=h[u];i;i=net[i])
48         if(!ct[i]){
49             deta[to[i]]+=deta[u],
50             ans[to[i]]+=deta[u];
51             if(!vis[to[i]]) vis[to[i]]=1,q.push(to[i]);
52         }
53         deta[u]=0;
54     }
55     printf("%d\n",ans[2]);
56 }
57 
58 int main(){
59     int u,v,c;
60     while(scanf("%d",&n)==1){
61         if(!n)break;
62         m=gi();
63         cnt=0;
64         memset(h,0,sizeof(h));
65         For(i,1,m) u=gi(),v=gi(),c=gi(),add(u,v,c),add(v,u,c);
66         pre(),solve();
67     }
68     return 0;
69 }

 

UVA10917 Walk Through the Forest

标签:广搜   记录   ora   can   -o   bsp   ems   fine   mem   

原文地址:https://www.cnblogs.com/five20/p/9348243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!