专栏首页算法修养PAT 甲级 1003Emergency(Dijkstra最短路)

PAT 甲级 1003Emergency(Dijkstra最短路)

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 of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output

2 4

题目的意思要求求出最短路的个数,和点的权值最大的值

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue>

using namespace std;
const int maxn=1e9;
struct Nod
{
  int value;
  int next;
  int weight;
}edge[505*2];
int head[505];
int tot;
void add(int x,int y,int z)
{
  edge[tot].value=y;
  edge[tot].weight=z;
  edge[tot].next=head[x];
  head[x]=tot++;
}
int vis[505];
int d[505];
int num[505];
int res[505];
int w[505];
int n,c1,c2,m;
struct Node
{
  int pos;
  int dis;
  Node(){};
  Node(int pos,int dis)
  {
    this->pos=pos;
    this->dis=dis;
  }
  friend bool operator <(Node a,Node b)
  {
    return a.dis>b.dis;
  }
};
void Dijkstra(int st)
{
  priority_queue<Node> q;
  q.push(Node(st,0));
  memset(vis,0,sizeof(vis));
  memset(num,0,sizeof(num));
  memset(res,0,sizeof(res));
  for(int i=0;i<=500;i++)
    d[i]=1e9;
  d[st]=0;num[st]=1;res[st]=w[st];
  while(!q.empty())
  {
    Node term=q.top();
    q.pop();
    if(vis[term.pos])
      continue;
    vis[term.pos]=1;
    for(int i=head[term.pos];i!=-1;i=edge[i].next)
    {
      int y=edge[i].value;
      if(d[y]>term.dis+edge[i].weight)
      {
        num[y]+=num[term.pos];
        res[y]=res[term.pos]+w[y];
        d[y]=term.dis+edge[i].weight;
        q.push(Node(edge[i].value,d[y]));
      }
      else if(d[y]==term.dis+edge[i].weight)
      {
                num[y]+=num[term.pos];
        res[y]=max(res[y],res[term.pos]+w[y]);
      }
    }
  }
}

int main()
{
  scanf("%d%d%d%d",&n,&m,&c1,&c2);
  for(int i=0;i<n;i++)
    scanf("%d",&w[i]);
  int x,y,z;
  memset(head,-1,sizeof(head));
  tot=0;
  for(int i=1;i<=m;i++)
  {
    scanf("%d%d%d",&x,&y,&z);
    add(x,y,z);
    add(y,x,z);
  }
  Dijkstra(c1);
  printf("%d %d\n",num[c2],res[c2]);
  return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • POJ--1699 Best Sequence(DP+dfs)

    Best Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions...

    ShenduCC
  • ZOJ 3705 Applications

    Recently, the ACM/ICPC team of Marjar University decided to choose some new memb...

    ShenduCC
  • FZU 2150 Fire Game(BFS)

    Problem 2150 Fire Game Accept: 1302    Submit: 4569 Time Limit: 1000 mSec    M...

    ShenduCC
  • 【Codeforces】1213B - Bad Prices

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    喜欢ctrl的cxk
  • HDU 3032 Nim or not Nim?(Multi-Nim)

    Problem Description Nim is a two-player mathematic game of strategy in which ...

    attack
  • Codeforces 626E Simple Skewness(暴力枚举+二分)

    E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megab...

    Angel_Kitty
  • 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛- A. Banana

    In the forest, there is a Banana Company that provides bananas from different pl...

    Enterprise_
  • POJ--1699 Best Sequence(DP+dfs)

    Best Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions...

    ShenduCC
  • 一道有趣的树状数组题

    Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social ga...

    ACM算法日常
  • hdu 2473 Junk-Mail Filter (并查集之点的删除)

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/...

    Gxjun

扫码关注云+社区

领取腾讯云代金券