九度OJ 1008

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
using namespace std;
vector<vector<int> > len(1001,  vector<int>(1001));
vector<vector<int> > cost(1001,  vector<int>(1001));
vector<int> distD(1001);
vector<int> distP(1001);
map<int, vector<int> > vertex;
bitset<1001> visit;
queue<int> q;
void findPath(int n, int start)
{
    visit.reset();
    vector<int>::iterator iter = vertex[start].begin();
    for(; iter != vertex[start].end(); ++iter)
    {
        distD[*iter] = len[start][*iter];
        distP[*iter] = cost[start][*iter];
        visit.set(*iter);
        q.push(*iter);
    }
    visit.set(start);
    distD[start] = 0;
    while(!q.empty())
    {
       int min = 100000000;
       int i = q.front();
       q.pop();
       vector<int>::iterator it = vertex[i].begin();
       for(; it != vertex[i].end(); ++it)
       {
           min = 100000000;
           if(!visit[*it])
           {
               for(vector<int>::iterator iter2 = vertex[*it].begin(); iter2 != vertex[*it].end(); ++iter2)
               {
                   if(visit[*iter2] && distD[*iter2] + len[*it][*iter2] < min)
                   {
 min = distD[*iter2] + len[*it][*iter2];
                       distD[*it] = min;
                       distP[*it] = distP[*iter2] + cost[*it][*iter2];
                   }
                   else if(visit[*iter2] && distD[*iter2] + len[*it][*iter2] == min)
                   {
                       if(distP[*it] > distP[*iter2] + cost[*it][*iter2])
                           distP[*it] = distP[*iter2] + cost[*it][*iter2];
                   }
               }
               visit.set(*it);
               q.push(*it);
           }
       }
    }
}
int main()
{
    //freopen("1.txt", "r", stdin);
    int n, m;
    while(cin >> n >> m,  m * n != 0)
    {
        len.clear();
        cost.clear();
        distD.clear();
        distP.clear();
        vertex.clear();
        len.resize(1001);
        cost.resize(1001);
        distD.resize(1001);
        distP.resize(1001);
        for(int i = 0; i < 1001; ++i)
 {
            len[i].resize(1001);
            cost[i].resize(1001);
        }
        for(int i = 0; i < 1001; ++i)
            for(int j = 0; j < 1001; ++j)
            {
                len[i][j] = 0;
                cost[i][j] = 0;
            }
        for(int i = 0; i < m; ++i)
        {
            int a, b, d, p;
            cin >> a >> b >> d >> p;
            vertex[a].push_back(b);
            vertex[b].push_back(a);
            len[a][b] = len[b][a] = d;
            cost[a][b] = cost[b][a] = p;
        }
        int start, end;
        cin >> start >> end;
        findPath(n, start);
        cout << distD[end] << " " << distP[end] << endl;
    }
}

你可能感兴趣的:(九度OJ 1008)