3.2.6 Sweet Butter 香甜的黄油

3.2.6 Sweet Butter 香甜的黄油

``````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号牧场最优
}*/

``````

``````/*
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];
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;
}
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);
}
dMin = INF;
for(int i = 1; i <= p; i++){
dijkstra(i);
}
printf("%d\n", dMin);
return 0;
}
``````

``````/*
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];
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;
}
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);