最短路-PAT (Advanced Level) Practice1003 Emergency

题意:给出来n个城市和每个城市的收获,以及两个城市之间的距离,求从某城市到目的城市的最短路有几条,以及其中收获最多的路线收获是多少

解析:最短路,稍微做一些改变,最短路学习的网址在这里【网址】

贴代码

#include
#include
#include
#include
#include
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 510;
bool vis[MAXN];
int num[MAXN];
int ans1[MAXN],ans2[MAXN];
void Dijkstra(int cost[][MAXN], int lowcost[],int n, int beg,int ends)
{
    //memset(lowcost,0,sizeof(lowcost));
    for(int i = 0; i < n; i ++)
    {
        vis[i] = false;
        lowcost[i] = INF;
    }
    lowcost[beg] = 0;
    ans1[beg] = 1;
    ans2[beg] = num[beg];
    for(int j = 0; j < n; j ++)
    {
        int k = -1;
        int Min = INF;
        for(int i = 0; i < n; i ++)
        {
            if(!vis[i] && lowcost[i] < Min)
            {
                Min = lowcost[i];
                k = i;
            }
        }
        if(k == -1)break;
        vis[k] = true;
        for(int i = 0; i < n; i ++)
        {
            if(!vis[i] )
            {
                if(lowcost[k] + cost[k][i] < lowcost[i])
                {
                    ans1[i] = ans1[k];
                    ans2[i] = ans2[k]+num[i];
                    lowcost[i] = lowcost[k] + cost[k][i];
                }
                else if(lowcost[k] + cost[k][i] == lowcost[i])
                {
                    ans1[i] += ans1[k];
                    ans2[i] = max(ans2[i],ans2[k]+num[i]);
                }

            }
        }
    }
}


int main()
{
    int N,M,C1,C2,c1,c2,l;
    cin >> N >> M >> C1 >> C2;

    int cost[MAXN][MAXN];
    int lowcost[MAXN];
    for(int i = 0; i < MAXN; i ++)
        for(int j = 0; j < MAXN; j ++)
            cost[i][j] = INF;
    for(int i = 0; i < N; i ++)scanf("%d",&num[i]);
    for(int i = 0; i < M; i ++)
    {
        scanf("%d%d%d",&c1,&c2,&l);
        cost[c1][c2] = l;
        cost[c2][c1] = l;
    }
    Dijkstra(cost,lowcost,N,C1,C2);
    cout << ans1[C2] << " " <

 

你可能感兴趣的:(OJ,图论)