Sweet Butter USACO 3.2

用n次djikstra 求最小和的思路,关键是如何优化,开始直接用数组存,case8 就过不去了,后来把extract-min换成

priority_queue 到case9还是没过去。

先贴个我的 ,在贴个大牛的

 

/*

ID: hubiao cave

PROG: butter

LANG: C++

*/

#include<iostream>
#include<fstream>
#include<cstring>
#include<queue>



using namespace std;
#define INFINITE 888888

int N,P,C;
int djistra(int origial);
int visit[801];
int counter[801];
int pastures[801][801];


struct mycom
{
    bool operator()(pair<int,int>p1,pair<int,int>p2)
    {
    if(p1.first!=p2.first)
        return p1.first>p2.first;
    else
    {
        return p1.second>p2.second;
    }

    }
};

int main()

{

    ifstream fin("butter.in");
    ofstream fout("butter.out");

    int index;
    int start,end,value;
    fin>>N>>P>>C;

    for(int i=0;i<N;i++)
    {
        fin>>index;
        counter[index]++;
    }


    for(int i=1;i<=P;i++)
        for(int j=1;j<=P;j++)
    {
        if(i==j)
            pastures[i][j]=0;
        else
            pastures[i][j]=INFINITE;
    }
    for(int i=0;i<C;i++)
    {
        fin>>start>>end>>value;
        pastures[start][end]=pastures[end][start]=value;
    }

    int minimum=INFINITE;

    for(int i=1;i<=P;i++)
    {
        int temp=djistra(i);
        if(minimum>temp)
            minimum=temp;
    }
    fout<<minimum<<endl;
    return 0;


}
int djistra(int original)
{
    priority_queue<pair<int,int> ,vector<pair<int,int> > ,mycom> myheap;

    int sum=0;
    int weight[801];
    for(int i=1;i<=P;i++)
    {
        //weight[i]=pastures[original][i];
        weight[i]=INFINITE;
        //myheap.push(make_pair(pastures[original][i],i));
    }
    weight[original]=0;
    myheap.push(make_pair(pastures[original][original],original));
    memset(visit,0,801*4);

    bool flag=true;
    while(!myheap.empty())
    {
    
        int marki=1;
        int value;
        //for(int i=1;i<=P;i++)
        //{
            //if(mini>weight[i]&&!visit[i])
            //{
                //marki=i;
            //
            pair<int, int> p=myheap.top();
            myheap.pop();
            marki=p.second;
            value=p.first;

        if(!visit[marki])
        {
        //}
        visit[marki]=1;
        for(int i=1;i<=P;i++)
        {
            if(value+pastures[marki][i]<weight[i])
            {
                weight[i]=value+pastures[marki][i];
                myheap.push(make_pair(weight[i],i));
            }
        }
        
        }

    //    flag=0;
        //for(int i=1;i<=P;i++)
        //    if(!visit[i])
                //flag=1;
    }

    for(int i=1;i<=P;i++)
    {
        sum+=counter[i]*weight[i];
    }
    return sum;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <queue>
using namespace std;
 
struct vertex {
    int dist;
    bool known;
} v[801];
 
int nCow, nPastures, nEdge;
int cow[503];
const int INF = 0x7FFFF;
// edge
vector < int >Map[801];
vector < int >val[801];
 
int dijkstra(int start)
{
    for (int i = 1; i <= nPastures; i++) {
        v[i].dist = INF;
        v[i].known = false;
    }
    priority_queue < pair < int, int > >heap;
    heap.push(make_pair(0, start));
    v[start].dist = 0;
 
    int u, w, cost;
    while (!heap.empty()) {
        u = heap.top().second;
        //cout << u << endl;
        heap.pop();
        if (!v[u].known) {
            v[u].known = true;
            for (unsigned int i = 0; i < Map[u].size(); i++) {
                w = Map[u][i];
                if (!v[w].known)
                    cost = val[u][i];
                if (v[w].dist > v[u].dist + cost) {
                    v[w].dist = v[u].dist + cost;
                    heap.push(make_pair(-v[w].dist, w));
                }
            }
        }
    }
 
    int totalDist = 0;
    for (int j = 0; j <= nCow; j++)
        totalDist += v[cow[j]].dist;
    //cout << start << ' '  << totalDist << endl;
    return totalDist;
}
 
int main()
{
    ifstream fin("butter.in");
    ofstream fout("butter.out");
 
    fin >> nCow >> nPastures >> nEdge;
    for (int i = 0; i < nCow; i++)
        fin >> cow[i];
    int l, r, d;
    for (int i = 0; i < nEdge; i++) {
        fin >> l >> r >> d;
        Map[l].push_back(r);
        Map[r].push_back(l);
        val[l].push_back(d);
        val[r].push_back(d);
    }
 
    int min = INF, walkDist;
    for (int i = 1; i <= nPastures; i++) {
        walkDist = dijkstra(i);
        if (min > walkDist)
            min = walkDist;
    }
 
    cout << min << endl;
    fout << min << endl;
    fin.close();
    fout.close();
    return 0;
}

 我改过之后的

/*

ID: hubiao cave

PROG: butter

LANG: C++

*/

#include<iostream>
#include<fstream>
#include<cstring>
#include<queue>
#include<vector>



using namespace std;
#define INFINITE 888888

int N,P,C;
int djistra(int origial);
int visit[801];
int counter[801];
int pastures[801][801];


vector<int> gmaps[801];
vector<int> gvalues[801];
struct mycom
{
    bool operator()(pair<int,int>p1,pair<int,int>p2)
    {
    if(p1.first!=p2.first)
        return p1.first>p2.first;
    else
    {
        return p1.second>p2.second;
    }

    }
};

int main()

{

    ifstream fin("butter.in");
    ofstream fout("butter.out");

    int index;
    int start,end,value;
    fin>>N>>P>>C;

    for(int i=0;i<N;i++)
    {
        fin>>index;
        counter[index]++;
    }


    for(int i=1;i<=P;i++)
        for(int j=1;j<=P;j++)
    {
        if(i==j)
            pastures[i][j]=0;
        else
            pastures[i][j]=INFINITE;
    }
    for(int i=0;i<C;i++)
    {
        fin>>start>>end>>value;
        //pastures[start][end]=pastures[end][start]=value;
        gmaps[start].push_back(end);
        gmaps[end].push_back(start);
        gvalues[start].push_back(value);
        gvalues[end].push_back(value);
    }

    int minimum=INFINITE;

    for(int i=1;i<=P;i++)
    {
        int temp=djistra(i);
        if(minimum>temp)
            minimum=temp;
    }
    fout<<minimum<<endl;
    return 0;


}
int djistra(int original)
{
    priority_queue<pair<int,int> ,vector<pair<int,int> > ,mycom> myheap;

    int sum=0;
    int weight[801];
    for(int i=1;i<=P;i++)
    {
        //weight[i]=pastures[original][i];
        weight[i]=INFINITE;
        //myheap.push(make_pair(pastures[original][i],i));
    }
    weight[original]=0;
    myheap.push(make_pair(pastures[original][original],original));
    memset(visit,0,801*4);

  //  bool flag=true;
    while(!myheap.empty())
    {
    
        int marki=1;
        int value;
        //for(int i=1;i<=P;i++)
        //{
            //if(mini>weight[i]&&!visit[i])
            //{
                //marki=i;
            //
        pair<int, int> p=myheap.top();
        myheap.pop();
        marki=p.second;
        value=p.first;

        if(!visit[marki])
        {
        //}
        visit[marki]=1;
        //for(int i=1;i<=P;i++)
        for(int i=0;i<gmaps[marki].size();i++)
        {
            int w=gmaps[marki][i];
            //if(value+pastures[marki][i]<weight[i])
            
            if(value+gvalues[marki][i]<weight[w])
            {
                weight[w]=value+gvalues[marki][i];
                myheap.push(make_pair(weight[w],w));
            }
        }
        
        }

    //    flag=0;
        //for(int i=1;i<=P;i++)
        //    if(!visit[i])
                //flag=1;
    }

    for(int i=1;i<=P;i++)
    {
        sum+=counter[i]*weight[i];
    }
    return sum;
}

 

posted @ 2013-10-09 23:16  cavehubiao  阅读(254)  评论(0编辑  收藏  举报