PAT (Advanced Level) 1003 dij变形 求最短路的数量 1046 思维 公式推导 1030 dij变形

点击打开链接

//dij变形
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int>PII;
const int maxv=500+5;
const int maxe=250000+5;
const int INF=0x3f3f3f3f;
struct Edge{
    int to,next,dist;
};
int cnt=0,head[maxv];
Edge edges[maxe];
void init(int u,int v,int k)
{
    edges[++cnt].next=head[u];
    edges[cnt].to=v;
    edges[cnt].dist=k;
    head[u]=cnt;
}
int v,e,s,en,d[maxv],d1[maxv];
LL res[maxv],num[maxv];
bool done[maxv];
void dij()
{
    memset(d,INF,sizeof(d));
    d[s]=0;
    d1[s]=1;
    priority_queue<PII,vector<PII>,greater<PII> >pq;
    while(!pq.empty()) pq.pop();
    memset(done,false,sizeof(done));
    pq.push(PII(0,s));
    while(!pq.empty())
    {
        PII k=pq.top();
        pq.pop();
        if(done[k.second]) continue;
        if(k.second==en) return;
        done[k.second]=true;
        for(int i=head[k.second];i!=0;i=edges[i].next)
        {
            if(!done[edges[i].to]&&d[k.second]+edges[i].dist<d[edges[i].to]) {
                d[edges[i].to]=k.first+edges[i].dist;
                pq.push(PII(d[edges[i].to],edges[i].to));
                res[edges[i].to]=max(1l*res[edges[i].to],1l*res[k.second]+num[edges[i].to]);
                d1[edges[i].to]=d1[k.second];
            }
            else if(!done[edges[i].to]&&d[k.second]+edges[i].dist==d[edges[i].to]) {
                res[edges[i].to]=max(1l*res[edges[i].to],1l*res[k.second]+num[edges[i].to]);
                d1[edges[i].to]+=d1[k.second];
            }
        }
    }
}
int main()
{
    memset(head,0,sizeof(head));
    scanf("%d%d%d%d",&v,&e,&s,&en);
    s++,en++;
    for(int i=1;i<=v;i++)
    {
        scanf("%lld",&num[i]);
    }
    memcpy(res,num,sizeof(num));
    for(int i=0;i<e;i++)
    {
        int v1,v2,di;
        scanf("%d%d%d",&v1,&v2,&di);
        init(v1+1,v2+1,di);
        init(v2+1,v1+1,di);
    }
    dij();
    printf("%d %lld\n",d1[en],res[en]);
    return 0;
}

PAT 1046

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
int main()
{
    int n,a[maxn];
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    LL sum1[maxn],sum2[maxn];
    sum1[1]=sum2[1]=0;
    for(int i=2;i<=n;i++)
    {
        sum1[i]=sum1[i-1]+1l*a[i-1];
    }
    for(int i=n;i>=2;i--)
    {
        if(i==n) sum2[i]=a[i];
        else {
            sum2[i]=sum2[i+1]+1l*a[i];
        }
    }
    int m;
    scanf("%d",&m);
    while(m--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        if(l>r) {
            swap(l,r);
        }
        else if(l==r) {
            printf("0\n");
            continue;
        }
        printf("%lld\n",min(sum1[r]-sum1[l],sum1[l]+sum2[r]));
    }
    return 0;
}

PAT 1030

dij变形,路径回溯

//dij变形+路径回溯
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
const int maxv=500+5;
const int maxe=250000+5;
const int INF=0x3f3f3f3f;
struct Edge{
    int to,next,weight;
};
Edge edges[maxe];
int head[maxv],cnt=0,cost[maxv][maxv],sum[maxv],parent[maxv];
void init(int u,int v,int dist)
{
    edges[++cnt].next=head[u];
    edges[cnt].to=v;
    edges[cnt].weight=dist;
    head[u]=cnt;
}
int v,e,st,en,d[maxv];
bool done[maxv];
void dij()
{
    memset(done,false,sizeof(done));
    priority_queue<PII,vector<PII>,greater<PII> >pq;
    while(!pq.empty()) pq.pop();
    pq.push(PII(0,st));
    memset(d,INF,sizeof(d));
    d[st]=0;
    sum[st]=0;
    parent[st]=-1;
    while(!pq.empty())
    {
        PII k=pq.top();
        pq.pop();
        if(k.second==en) return;
        if(done[k.second]) continue;
        done[k.second]=true;
        for(int i=head[k.second];i!=0;i=edges[i].next)
        {
            if(!done[edges[i].to]&&d[edges[i].to]>k.first+edges[i].weight) {
                d[edges[i].to]=k.first+edges[i].weight;
                pq.push(PII(d[edges[i].to],edges[i].to));
                sum[edges[i].to]=sum[k.second]+cost[k.second][edges[i].to];
                parent[edges[i].to]=k.second;
            }
            else if(!done[edges[i].to]&&d[edges[i].to]==k.first+edges[i].weight) {
                if(sum[edges[i].to]>sum[k.second]+cost[k.second][edges[i].to]) {
                    sum[edges[i].to]=sum[k.second]+cost[k.second][edges[i].to];
                    parent[edges[i].to]=k.second;
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&v,&e,&st,&en);
    st++,en++;
    memset(head,0,sizeof(head));
    memset(cost,INF,sizeof(cost));
    for(int i=1;i<=v;i++)
        cost[i][i]=0;
    for(int i=0;i<e;i++)
    {
        int v1,v2,dist,c;
        scanf("%d%d%d%d",&v1,&v2,&dist,&c);
        v1++,v2++;
        init(v1,v2,dist);
        init(v2,v1,dist);
        cost[v1][v2]=cost[v2][v1]=min(cost[v1][v2],c);
    }
    dij();
    vector<int>path;
    path.clear();
    int now=en;
    path.push_back(now);
    while(parent[now]!=-1)
    {
        path.push_back(parent[now]);
        now=parent[now];
    }
    reverse(path.begin(),path.end());
    for(int i=0;i<path.size();i++)
    {
        printf("%d ",path[i]-1);
    }
    printf("%d %d\n",d[en],sum[en]);
    //cout<<d[en]<<" "<<sum[en]<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值