pat-1003* Emergency

1003:

dfs遍历所有路径。
#include <iostream>
#include <vector>
using namespace std;

int city;
int road;
int collect=0;
int maxcollect=0;
int shortnum=0;
int used[501];
int team[501]={0};
int minpass[501]={0};
int shortdis=100000000;
int curdis=0;
int map[501][501]={0};
int S,T;
vector<int> path;

void dfs(int u)
{
    if(u==T)
    {
		for(int i=0;i<path.size();i++)
        {
			collect+=team[path[i]];
        }

        for(int i=0;i<path.size()-1;i++)
        {
			curdis+=map[path[i]][path[i+1]];
        }
		
		if(curdis<shortdis)
		{
			shortnum=1;
			shortdis=curdis;
			maxcollect=collect;
		}
		else if(curdis==shortdis)
		{
			shortnum++;
			if(collect>maxcollect)
				maxcollect=collect;
		}

		curdis=0;
		collect=0;
    }
    else
    {
        for(int v=0;v<501;v++)
		if(map[u][v])
        {
            if(used[v]==false) //avoid loop
            {
                used[v]=true;
                path.push_back(v);
                dfs(v);
                path.pop_back();
                used[v]=false;
            }
        }
    }
}

int main()
{
	int x;
	int y;
	int value;

	int i;
	int j;
	cin>>city;
	cin>>road;
	cin>>S;
	cin>>T;

	for(i=0;i<city;i++)
		cin>>team[i];

	for(i=0;i<road;i++)
	{
		cin>>x;
		cin>>y;
		cin>>value;
		map[x][y]=value;
		map[y][x]=value;
	}

	path.push_back(S);
    used[S] = 1;
    dfs(S);
    used[S] = 0;
    path.pop_back();

	printf("%d %d",shortnum,maxcollect);
    return 0;

}

你可能感兴趣的:(merge)