1003. Emergency (25)

</pre>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.<p></p><p style="margin-top:0px; margin-bottom:1.5em; padding-top:0px; padding-bottom:0px; border:0px; line-height:18px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; vertical-align:baseline; color:rgb(51,51,51); background-color:rgb(250,250,250)"></p><p style="margin-top:0px; margin-bottom:1.5em; padding-top:0px; padding-bottom:0px; border:0px; line-height:18px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; vertical-align:baseline; color:rgb(51,51,51); background-color:rgb(250,250,250)"><strong>Input</strong></p><p style="margin-top:0px; margin-bottom:1.5em; padding-top:0px; padding-bottom:0px; border:0px; line-height:18px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; vertical-align:baseline; color:rgb(51,51,51); background-color:rgb(250,250,250)">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.</p><p style="margin-top:0px; margin-bottom:1.5em; padding-top:0px; padding-bottom:0px; border:0px; line-height:18px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; vertical-align:baseline; color:rgb(51,51,51); background-color:rgb(250,250,250)"><strong>Output</strong></p><p style="margin-top:0px; margin-bottom:1.5em; padding-top:0px; padding-bottom:0px; border:0px; line-height:18px; font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; vertical-align:baseline; color:rgb(51,51,51); background-color:rgb(250,250,250)">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.</p><span style="color:rgb(51,51,51); font-family:'Droid Sans',Verdana,'Microsoft YaHei',Tahoma,sans-serif; line-height:18px; background-color:rgb(250,250,250)">Sample Input</span><pre style="margin-top:1.5em; margin-bottom:1.5em; padding:0px; border:0px; line-height:18px; font-family:'Droid Sans Mono',Consolas,'Courier New',monospace; vertical-align:baseline; overflow:auto; color:rgb(51,51,51); background-color:rgb(250,250,250)">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

Dijkstra解法

#include <iostream>
#include <cstdio>
#include <cstring>

#define MAXD 0x7ffffff

using namespace std;

int matrix[510][510];//邻接矩阵
int rescue[510];
int dist[510];//保存最短路径长度
//int path[10][10];//路径
bool mark[510];//若final[i] = 1则说明 顶点vi已在集合S中
int n,m,s,e;//n顶点个数
int cnt[510];//源点到其他的点的最短路径的条数
int sum[510];//到某点的最大的权值和
int now;

void input()
{
    freopen("1003.in","r",stdin);
    scanf("%d%d%d%d",&n,&m,&s,&e);
    for(int i = 0; i < n;i++)
    {
        scanf("%d",&rescue[i]);
        mark[i] = false;
        dist[i] = MAXD;
        for(int j = 0;j < n;j++)
        {
            matrix[i][j] = MAXD;
        }
    }

    memset(cnt,0,sizeof(cnt));
    memset(sum,0,sizeof(sum));

    for(int i = 0 ; i < m;i++)
    {
        int c1,c2,l;
        scanf("%d%d%d",&c1,&c2,&l);
        matrix[c1][c2] = l;
        matrix[c2][c1] = l;
    }

}

void dijkstra(int first,int last)
{
	//初始化
	for(int i = 0;i < n;i++)
	{
		dist[i] = MAXD;
		mark[i] = false;
	}
	dist[first] = 0;
    cnt[first] = 1;
    sum[first] = rescue[first];

	for(int i = 0;i < n;i++)
	{

		//找到最小值
		int mindist = MAXD;
		for(int j = 0; j < n;j++)
		{
			if(mark[j] == false)
			{
				if(dist[j] < mindist)
				{
					mindist = dist[j];
					now = j;
				}
			}
		}

		mark[now] = true;
		//更新
		for(int j = 0 ; j < n;j++)
		{
            if( mark[j] == false && matrix[now][j] < MAXD )
            {
                if(dist[j] > dist[now] + matrix[now][j])
                {
                    dist[j] = dist[now] + matrix[now][j];
                    sum[j] = sum[now] + rescue[j];
                    cnt[j] = cnt[now];
                }
                else if(dist[j] == dist[now] + matrix[now][j])
                {
                    cnt[j] += cnt[now];
                    if(sum[j] < sum[now] + rescue[j])
                    {
                        sum[j] = sum[now] + rescue[j];
                    }
                }
            }
		}

		//if(now == last) return;
	}
}

int main()
{
    input();
    dijkstra(s,e);
    printf("%d %d\n",cnt[e],sum[e]);
    return 0;
}


DFS解法

/* 1003 PAT
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
先将所有的路线存放在ans中,再从中选出最优解
*/
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstring>

#define MAXD 0x7fffffff

using namespace std;

typedef struct Road{
    int dist;
    int team;
}road;

vector<road> ans;

int matrix[510][510];//邻接矩阵
int rescue[510];
//int path[10][10];//路径
bool visited[510];//若visited[i] = true则说明 顶点vi已在集合S中
int n,m,s,e;//n顶点个数

bool cmp(road a,road b)
{
    if(a.dist != b.dist)
    {
        return a.dist < b.dist;
    }
    else if(a.team != b.team)
    {
        return a.team > b.team;
    }
}

void input()
{
    //freopen("1003.in","r",stdin);
    scanf("%d%d%d%d",&n,&m,&s,&e);
    for(int i = 0; i < n;i++)
    {
        scanf("%d",&rescue[i]);
        visited[i] = false;
        for(int j = 0;j < n;j++)
        {
            matrix[i][j] = MAXD;
        }
    }

    for(int i = 0 ; i < m;i++)
    {
        int c1,c2,l;
        scanf("%d%d%d",&c1,&c2,&l);
        matrix[c1][c2] = l;
        matrix[c2][c1] = l;
    }

    visited[s] = true;
}

void dfs(int s,int e,int dist,int team)
{
    if(s == e)
    {
        road temp;
        temp.dist = dist;
        temp.team = team;
        ans.push_back(temp);
    }
    else
    {
        for(int i = 0; i < n;i++)
        {
            if(matrix[s][i] < MAXD && visited[i] == false)
            {
                visited[i] = true;
                dfs(i,e,dist+matrix[s][i],team+rescue[i]);
                visited[i] = false;
            }
        }
    }
}

int main()
{
    input();
//    if(n==1)
//	{
//		cout<<"1 "<<rescue[0]<<endl;
//		return 0;
//	}
    dfs(s,e,0,rescue[s]);
    sort(ans.begin(),ans.end(),cmp);
    int cnt = 0;
    int mindist = ans[0].dist;
    while(mindist == ans[cnt].dist && cnt < ans.size())
    {
        cnt++;
    }
    printf("%d %d",cnt,ans[0].team);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值