hdoj1571
下沙小面的(1)
代码
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int NCity, T, dist[30][30], K, pn, ans, P[7], now; //dist距离,pn人数,now现在的车站
int main()
{
while (scanf("%d", &NCity) != EOF && NCity) {
pn = 0, now = 0, ans = 0;
for (int i = 0; i < NCity; ++i)
for (int j = 0; j < NCity; ++j)
scanf("%d", &dist[i][j]);
scanf("%d", &K);
char ch1, ch2;
while (K--) {
cin >> ch1 >> ch2;
if (ch1 == 'U') { //有人上车
scanf("%d", &T);
if (pn == 7 || T == now)
continue; //人満
P[pn] = T;
pn++;
} else { //开车
if (pn == 0)
continue;
ans += dist[now][P[0]];
now = P[0];
int tem[7];
int ptem = 0;
for (int pos = 0; pos < pn; ++pos) {
if (P[pos] == now) {
continue;
}
tem[ptem++] = P[pos];
}
for (int pos = 0; pos < ptem; ++pos)
P[pos] = tem[pos];
pn = ptem;
}
}
printf("%d\n", ans);
}
}
#include<stdio.h>
#include<string.h>
using namespace std;
int NCity, T, dist[30][30], K, pn, ans, P[7], now; //dist距离,pn人数,now现在的车站
int main()
{
while (scanf("%d", &NCity) != EOF && NCity) {
pn = 0, now = 0, ans = 0;
for (int i = 0; i < NCity; ++i)
for (int j = 0; j < NCity; ++j)
scanf("%d", &dist[i][j]);
scanf("%d", &K);
char ch1, ch2;
while (K--) {
cin >> ch1 >> ch2;
if (ch1 == 'U') { //有人上车
scanf("%d", &T);
if (pn == 7 || T == now)
continue; //人満
P[pn] = T;
pn++;
} else { //开车
if (pn == 0)
continue;
ans += dist[now][P[0]];
now = P[0];
int tem[7];
int ptem = 0;
for (int pos = 0; pos < pn; ++pos) {
if (P[pos] == now) {
continue;
}
tem[ptem++] = P[pos];
}
for (int pos = 0; pos < ptem; ++pos)
P[pos] = tem[pos];
pn = ptem;
}
}
printf("%d\n", ans);
}
}