PAT (Advanced Level) 1003. Emergency (25)

最短路+dfs

先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了。

也可以对DAG进行拓扑排序,然后DP求解。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;

const int INF=0x7FFFFFFF;
const int maxn=550;
int n,m,C1,C2,tot,ans1,ans2;
int val[maxn];
struct Edge
{
    int u,v,L;
}e[600000];
int f[600000];
vector<int>g[maxn];
int dis[2][maxn];
int h[maxn];

void init()
{
    tot=0;
    for(int i=0;i<=500;i++) g[i].clear();
    memset(h,0,sizeof h);
}

void add(int a,int b,int c)
{
    e[tot].u=a,e[tot].v=b,e[tot].L=c;
    g[a].push_back(tot);
    tot++;
}

void read()
{
    scanf("%d%d%d%d",&n,&m,&C1,&C2);
    for(int i=0;i<n;i++) scanf("%d",&val[i]);
    for(int i=1;i<=m;i++)
    {
        int u,v,L; scanf("%d%d%d",&u,&v,&L);
        add(u,v,L);
        add(v,u,L);
    }
}

void SPFA(int f,int st)
{
    for(int i=0;i<=500;i++) dis[f][i]=INF;
    dis[f][st]=0;
    int flag[maxn]; memset(flag,0,sizeof flag);
    queue<int>Q; Q.push(st); flag[st]=1;
    while(!Q.empty())
    {
        int head= Q.front(); Q.pop(); flag[head]=0;
        for(int i=0;i<g[head].size();i++)
        {
            int id=g[head][i];
            if(dis[f][head]+e[id].L<dis[f][e[id].v])
            {
                dis[f][e[id].v]=dis[f][head]+e[id].L;
                if(flag[e[id].v]==0)
                {
                    Q.push(e[id].v);
                    flag[e[id].v]=1;
                }
            }
        }
    }
}

void dfs(int x,int v)
{
    if(x==C2)
    {
        ans1++; ans2=max(ans2,v);
        return ;
    }

    for(int i=0;i<g[x].size();i++)
    {
        int id=g[x][i];
        if(f[id]==0) continue;
        dfs(e[id].v,v+val[e[id].v]);
    }
}

void work()
{
    int len=dis[0][C2];

    for(int i=0;i<tot;i++)
        if(dis[0][e[i].u]+e[i].L+dis[1][e[i].v]==len)
            f[i]=1;

    ans1=0; dfs(C1,val[C1]);
    printf("%d %d\n",ans1,ans2);
}

int main()
{
    init();
    read();
    SPFA(0,C1);
    SPFA(1,C2);
    work();
    return 0;
}
时间: 2024-11-05 17:45:56

PAT (Advanced Level) 1003. Emergency (25)的相关文章

PAT Advanced level 1003 Emergency

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are

【PAT甲级】1003 Emergency (25分)

1003 Emergency (25分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between an

PAT (Advanced Level) 1078. Hashing (25)

二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; const int

PAT (Advanced Level) 1029. Median (25)

scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<queue> #include<iostream> #include<algorithm> using namespace std; const int maxn=1000000+10; int a[maxn],b

PAT (Advanced Level) 1070. Mooncake (25)

简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; double eps=1e-7

PAT (Advanced Level) 1032. Sharing (25)

简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; const int maxn=100000+10

1003. Emergency (25)——PAT (Advanced Level) Practise

题目信息: 1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads.

PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d