PAT:1003. Emergency (25) AC

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXV=510;
const int INF=0x3fffffff;
int n,m,c1,c2;

bool vis[MAXV];
int G[MAXV][MAXV];      //城市间距离
int weight[MAXV];      //每个城市的救援人数
int d[MAXV];        //最短距离
int w[MAXV];        //可以带的最多人
int num[MAXV];        //最短路径条数

void Dijkstra(int s)
{
  memset(num,0,sizeof(num));
  memset(vis,false,sizeof(vis));
  fill(d,d+MAXV,INF);
  memset(w,0,sizeof(w));
  d[s]=0;            //本城市到本城市距离为0
  w[s]=weight[s];        //自己城市的救援队人数
  num[s]=1;          //到自己城市最短路径为1条

  for(int i=0 ; i<n ; ++i)
  {
    int MIN=INF,u=-1;
    for(int j=0 ; j<n ; ++j)  //找最小d
    {
      if(vis[j]==false && MIN>d[j])
      {
        MIN=d[j];
        u=j;
      }
    }

    if(-1==u)          //图有不连通的部分
      return;
    vis[u]=true;
    for(int v=0 ; v<n ; ++v)
    {
      if(vis[v]==false && G[u][v]!=INF)
      {
        if(d[v]>d[u]+G[u][v])
        {
          d[v]=d[u]+G[u][v];        //更新最短距离
          w[v]=w[u]+weight[v];      //更新救援人数
          num[v]=num[u];          //【warning】到新最短路径,当然是u的条数
        }
        else if(d[v]==d[u]+G[u][v])
        {
          num[v]+=num[u];          //【warning】最短路径数为u的条数+v的条数
          if(w[v]<w[u]+weight[v])
          {
            w[v]=w[u]+weight[v];    //路径相同,存入最多的救援队数量
          }
        }
      }
    }
  }
}

int main()
{
  fill(G[0],G[0]+MAXV*MAXV,INF);
  scanf("%d%d%d%d",&n,&m,&c1,&c2);
  for(int i=0 ; i<n ; ++i)      //存入每个城市救援队人数
    scanf("%d",&weight[i]);
  for(int i=0 ; i<m ; ++i)      //存入城市间距离
  {
    int u,v;
    scanf("%d%d",&u,&v);
    scanf("%d",&G[u][v]);
    G[v][u]=G[u][v];
  }
  Dijkstra(c1);
  printf("%d %d\n",num[c2],w[c2]);
  return 0;
}
时间: 2024-10-11 21:56:22

PAT:1003. Emergency (25) AC的相关文章

PAT 甲级 1003. Emergency (25)

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:1029. Median (25) AC

#include<stdio.h> #include<algorithm> using namespace std; int arr1[1000066]; int arr2[1000066]; int main() { int n1,n2; scanf("%d",&n1); for(int i=0 ; i<n1 ; ++i) { scanf("%d",&arr1[i]); } scanf("%d",&

PAT:1020. 月饼 (25) AC

#include<stdio.h> #include<algorithm> using namespace std; struct cake { double amount; double sum_price,price; }M[1010]; bool cmp(cake a,cake b) { return a.price>b.price; } int main() { double need=0; int kinds=0; scanf("%d%lf",&

PAT:1032. Sharing (25) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { char data; int next; bool tag; }Node[100066]; int main() { int add1,add2,n; //连表1首地址, scanf("%d%d%d",&add1,&add2,&n); memset(Node,0,sizeof(Node))

PAT:1070. Mooncake (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct moomcake { double store; //[cation]这个可能是小数! double sumPrice; double solePrice; }M[1010]; bool cmp(moomcake a,moomcake b) { return a.solePrice>b.soleP

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 1003 Emergency (25)(25 分)

1003 Emergency (25)(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 betwe

【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 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