3.2.6 Sweet Butter 香甜的黄油

3.2.6 Sweet Butter 香甜的黄油

时间: 1ms        内存:64M

描述:

农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他将付出额外的费用在奶牛上。 农夫John很狡猾。像以前的Pavlov,他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。 农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)

输入:

第一行: 三个数:奶牛数N,牧场数(2<=P<=800),牧场间道路数C(1<=C<=1450) 第二行到第N+1行: 1到N头奶牛所在的牧场号 第N+2行到第N+C+1行: 每行有三个数:相连的牧场A、B,两牧场间距离D(1<=D<=255),当然,连接是双向的

输出:

一行 输出奶牛必须行走的最小的距离和

示例输入:

3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5
/*
{样例图形
          P2  
 P1 @--1--@ C1
     \    |\
      \   | \
       5  7  3
        \ |   \
         \|    \ C3
       C2 @--5--@
          P3    P4
}
*/

示例输出:

8
/*{说明:
  放在4号牧场最优
}*/

提示:

参考答案(内存最优[1328]):

/*
ID:zhaofuk1
PROG: butter
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
const int INF = (1<<29);
const int MAXN = 810;
int n, p, c, e, dMin;
int d[MAXN], isIn[MAXN];
bool done[MAXN];
int head[MAXN];
typedef struct{
    int u, v, next, c;
}Edge;
Edge nEdge[3000];
typedef pair<int, int> pill;
priority_queue<pill, vector<pill>, greater<pill> > q;
void AddEdge(int u, int v, int c)
{
    nEdge[e].u = u; nEdge[e].v = v; nEdge[e].c = c;
    nEdge[e].next = head[u]; head[u] = e++;
}
void dijkstra(int s)
{
    pill u;
    int x, sum;
    for(int i = 1; i <= p; i++) d[i] = (i == s) ? 0 : INF;
    memset(done, 0, sizeof(done));
    q.push(make_pair(d[s], s));
    while(!q.empty()){
        u = q.top(); q.pop();
        x = u.second;
        if(done[x]) continue;
        done[x] = true;
        for(int ee = head[x]; ee != -1; ee = nEdge[ee].next){
            if(d[nEdge[ee].v] > d[x] + nEdge[ee].c){
                d[nEdge[ee].v] = d[x] + nEdge[ee].c;
                q.push(make_pair(d[nEdge[ee].v], nEdge[ee].v));
            }
        }
    }
    sum = 0;
    for(int i = 1; i <= p; i++){
        sum += d[i] * isIn[i];
    }
    //printf(" sum = %d dmin=%d\n", sum, dMin);
    if(dMin > sum) dMin = sum;
}

int main()
{
    int x, y, z;
    //freopen("butter.in", "r", stdin);
    //freopen("butter.out", "w", stdout);
    scanf("%d %d %d", &n, &p, &c);
    memset(isIn, 0, sizeof(isIn));
    for(int i = 0; i < n; i++){
        scanf("%d", &x);
        isIn[x]++;
    }
    for(int i = 1; i <= p; i++) head[i] = -1;
    e = 0;
    for(int i = 0; i < c; i++){
        scanf("%d %d %d", &x, &y, &z);
        AddEdge(x, y, z);
        AddEdge(y, x, z);
    }
    dMin = INF;
    for(int i = 1; i <= p; i++){
        dijkstra(i);
    }
    printf("%d\n", dMin);
    return 0;
}

参考答案(时间最优[608]):

/*
ID:zhaofuk1
PROG: butter
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
const int INF = (1<<29);
const int MAXN = 810;
int n, p, c, e, dMin;
int d[MAXN], isIn[MAXN];
bool done[MAXN];
int head[MAXN];
typedef struct{
    int u, v, next, c;
}Edge;
Edge nEdge[3000];
typedef pair<int, int> pill;
priority_queue<pill, vector<pill>, greater<pill> > q;
void AddEdge(int u, int v, int c)
{
    nEdge[e].u = u; nEdge[e].v = v; nEdge[e].c = c;
    nEdge[e].next = head[u]; head[u] = e++;
}
void dijkstra(int s)
{
    pill u;
    int x, sum;
    for(int i = 1; i <= p; i++) d[i] = (i == s) ? 0 : INF;
    memset(done, 0, sizeof(done));
    q.push(make_pair(d[s], s));
    while(!q.empty()){
        u = q.top(); q.pop();
        x = u.second;
        if(done[x]) continue;
        done[x] = true;
        for(int ee = head[x]; ee != -1; ee = nEdge[ee].next){
            if(d[nEdge[ee].v] > d[x] + nEdge[ee].c){
                d[nEdge[ee].v] = d[x] + nEdge[ee].c;
                q.push(make_pair(d[nEdge[ee].v], nEdge[ee].v));
            }
        }
    }
    sum = 0;
    for(int i = 1; i <= p; i++){
        sum += d[i] * isIn[i];
    }
    //printf(" sum = %d dmin=%d\n", sum, dMin);
    if(dMin > sum) dMin = sum;
}

int main()
{
    int x, y, z;
    //freopen("butter.in", "r", stdin);
    //freopen("butter.out", "w", stdout);
    scanf("%d %d %d", &n, &p, &c);
    memset(isIn, 0, sizeof(isIn));
    for(int i = 0; i < n; i++){
        scanf("%d", &x);
        isIn[x]++;
    }
    for(int i = 1; i <= p; i++) head[i] = -1;
    e = 0;
    for(int i = 0; i < c; i++){
        scanf("%d %d %d", &x, &y, &z);
        AddEdge(x, y, z);
        AddEdge(y, x, z);
    }
    dMin = INF;
    for(int i = 1; i <= p; i++){
        dijkstra(i);
    }
    printf("%d\n", dMin);
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注