ZJOJ Highway Project 3946(单源最短路+贪心)

本文介绍了一种用于解决大规模图上最短路径问题的算法——SPFA(Shortest Path Faster Algorithm)。该算法适用于图中存在大量节点和边的情况,并且特别关注如何在保证效率的同时找到最优路径。文章详细解释了SPFA算法的实现过程,并通过具体实例说明了其应用场景。

大意:有n,m表示n个点,m个边,然后就是m条边的信息,分别是x,y,ti,cost.点xy间的路耗费时间是ti,金钱是cost.因为点和边范围为10^5,所以想到复杂度小的SPFA(O(E)).

思路:直接是SPFA边求最短的时间的路,同时求耗费的金钱。但是注意耗费的金钱,cost[]数组不能够存从源点到当前点的耗费,因为若
两条路径时间是相等的,但是一条路是好多节点组成的但是耗费多。但是另一个是节点少耗费少。肯定选节点少的路径。

#include<map>
#include<queue>
#include<cmath>
#include<cstdio>
#include<stack>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
#define LL __int64
#define inf 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;

struct node{
    ll to,next,ti,cost;
}q[100010*5];

ll head[100010*5],cnt,dis[100010],cost[100010],n;
bool vis[100010];

void bu(ll a,ll b,ll c,ll d){
    q[cnt].to = b;
    q[cnt].ti = c;
    q[cnt].cost = d;
    q[cnt].next = head[a];
    head[a] = cnt++;
}

void SPFA(){
    memset(vis,false,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    memset(cost,inf,sizeof(cost));
    queue<ll>que;
    while(!que.empty())
        que.pop();
    dis[0] = cost[0] = 0;
    vis[0] = true;

    que.push(0);
    while(!que.empty()){
        ll u = que.front();
        vis[u] = false;
        que.pop();
        for( int i = head[u];~i;i = q[i].next ){
            int v = q[i].to;
            if( (dis[v] > dis[u] + q[i].ti)||(dis[v] == dis[u] + q[i].ti&&cost[v] > q[i].cost)){
                dis[v] = dis[u] + q[i].ti;
                cost[v] = q[i].cost;
                if(!vis[v]){
                    vis[v] = true;
                    que.push(v);
                }
            }
        }
    }

    ll ans1,ans2;
    ans1=ans2=0;
    for(ll i = 0;i < n ;++ i){
        ans1 += dis[i];
        ans2 += cost[i];
    }
    printf("%lld %lld\n",ans1,ans2);
}

int main(){
    ll m,i,j,k,cla,a,b,c,d;
    scanf("%lld",&cla);
    while(cla--){
        memset(head,-1,sizeof(head));
        cnt = 0;
        scanf("%lld%lld",&n,&m);
        for(i = 0;i <m;++ i){
            scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
            bu(a,b,c,d);bu(b,a,c,d);
        }
        SPFA();
    }
    return 0;
}
JZOJ 5186. 【NOIP2017提高组模拟6.30】tty's home 原创 于 2017-06-30 20:39:51 发布 · 411 阅读 · 1 · 1 · CC 4.0 BY-SA版权 文章标签: #动态规划 #树形DP 动态规划 同时被 2 个专栏收录 58 篇文章 订阅专栏 树形DP 12 篇文章 订阅专栏 本文介绍了一种利用树形动态规划(DP)解决特定匹配数问题的方法。通过定义节点匹配数并建立转移方程,文章详细阐述了如何计算不受限及受限条件下的匹配总数,并给出了解决方案的具体步骤。 Description Input Output Sample Input input 1: 5 1 1 1 1 1 1 2 2 3 3 4 4 5 input 2: 5 0 1 0 1 0 1 2 2 3 3 4 4 5 Sample Output output 1: 15 output 2: 12 Data Constraint Solution 正难则反,这题我们可以转换一下思路。姑且称 可能 符合条件的一种方案为 一个匹配 。 设 A= 不加限制的匹配数(总量), B= 不符合条件的匹配数(部分),则答案即为 A-B 。 那么我们如何求出 A 和 B 呢?可以用到 树形DP 的方法。 设 FiFi 表示以 ii 为根的子树所产生的匹配数。则有转移方程式: Fi=Fi∗Fj+Fj(j∈Soni) Fi=Fi∗Fj+Fj(j∈Soni) (逐一组合) 处理完上述式子后,再有:Fi=Fi+1 Fi=Fi+1 (单独自己也是一种匹配) 后可以求出 A : A=∑i=1nFi A=∑i=1nFi 求 B 也同理,不过当 jj 为值点时不转移 ii 、当 ii 为值点时 FiFi 不 +1 。 后 A、B 相减即可得出答案,总时间复杂度是 O(N)O(N) 。 Code #include<cstdio> #include<cstring> using namespace std; const int N=100001,mo=998244353; int tot,mx=-2147483647; int first[N],next[N<<1],en[N<<1]; int a[N]; long long f[N],g[N]; long long ans; inline int read() { int X=0,w=1; char ch=0; while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();} while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar(); return X*w; } inline void insert(int x,int y) { next[++tot]=first[x]; first[x]=tot; en[tot]=y; } inline void dfs1(int x,int y) { for(int i=first[x];i;i=next[i]) if(en[i]!=y) { dfs1(en[i],x); (f[x]+=f[x]*f[en[i]]%mo+f[en[i]])%=mo; } ans+=++f[x]; } inline void dfs2(int x,int y) { for(int i=first[x];i;i=next[i]) if(en[i]!=y) { dfs2(en[i],x); if(a[en[i]]<mx) (g[x]+=g[x]*g[en[i]]%mo+g[en[i]])%=mo; } if(a[x]<mx) (ans+=mo-++g[x])%=mo; } int main() { int n=read(); for(int i=1;i<=n;i++) if((a[i]=read())>mx) mx=a[i]; for(int i=1;i<n;i++) { int x=read(),y=read(); insert(x,y); insert(y,x); } dfs1(1,0),dfs2(1,0); printf("%lld",ans); return 0; } 一键获取完整项目代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 Felix-Lee 关注 1 1 0 分享 专栏目录 1.rar(noip2017测试数据) 07-06 【描述】"noip测试数据 noip2017提高组复赛"表明这些数据是为NOIP 2017年提高组复赛准备的。提高组是NOIP竞赛中的一个级别,针对有一定编程基础和经验的学生,比赛难度相对于入门组更高。复赛通常在初赛之后进行,... NOIP2017提高组C++试题(初赛) 10-20 NOIP2017提高组C++试题(初赛)是一套针对高级别选手的竞赛题目,包含了多项计算机科学的基础知识点,这些知识点覆盖了算法、数据结构、逻辑推理、概率计算等。 1. NOIP竞赛不再支持Pascal语言的时间点,这是一个... 【ZJOJ5186】【NOIP2017提高组模拟6.30】tty's home chen1352 509 如果直接求方案数很麻烦。 但是,我们可以反过来做:先求出所有的方案数,在减去不包含的方案数。 由于所有的路径连在一起, 于是设f[i]表示以i为根的子树中,连接到i的方案数设f[i]表示以i为根的子树中,连接到i的方案数 则f[i]=f[son]+(f[i]+1)f[i]=f[son]+(f[i]+1)表示从子树son分别到i和i其他儿子的子树的路径方案数。 由于每棵子树互不影响, 【NOIP2017提高组模拟6.30 ———————————————— 版权声明:本文为优快云博主「Felix-Lee」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.youkuaiyun.com/liyizhixl/article/details/74012007
最新发布
10-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值