这道题只有60分,我对比了网上的博客,也没发现有什么不一样的地方,希望有大佬告知原因!谢谢!
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 1050;
bool g[N][N]; // true 代表不能走
int n, m, k , d;
typedef pair<int, int> PII;
int min_cost[N];
typedef pair<int, pair<int, int> > PIII;
PII store[N];
PIII buyer[N];
queue<PII> q;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int mincost[N][N];
bool st[N][N];
//void bfs(int a, int b)
void bfs()
{
// q.push({a, b});
// mincost[a][b] = 0;
// st[a][b] = true;
while(q.size())
{
PII t = q.front();
q.pop();
int x = t.first; int y = t.second;
// cout<<x<<" "<<y<<endl;
int cost = mincost[x][y];
for(int i = 0; i<4; i++)
{
int xx = x + dx[i], yy = y + dy[i];
// cout<<"test:"<<xx<<" "<<yy<<" "<<st[xx][yy] <<" "<< g[xx][yy]<<endl;
if(!st[xx][yy] && xx > 0 && xx <= n && yy >0 && yy <=n && g[xx][yy] == 0)
{
// cout<<"aaa"<<xx<<" "<<yy<<endl;
mincost[xx][yy] = min( cost + 1, mincost[xx][yy] );
q.push({xx, yy});
st[xx][yy] = true;
}
}
}
}
int main()
{
memset(mincost, 0x3f, sizeof (mincost));
scanf("%d%d%d%d", &n, &m, &k, &d);
for(int i = 0; i<m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
store[i].first = n+1 - b; store[i].second = a;
}
for(int i = 0; i<k; i++)
{
int a , b ,c;
scanf("%d%d%d", &a, &b, &c);
buyer[i].first = n+1 - b; buyer[i].second.first = a; buyer[i].second.second = c;
}
for(int i = 0; i<d; i++)
{
int a;int b;
scanf("%d%d", &a, &b);
g[n+ 1 - b][a] = true;;
}
for(int i = 0; i < m ; i++)
{
// memset(st, 0, sizeof(st));
int a = store[i].first; int b= store[i].second;
q.push({a, b});
mincost[a][b] = 0;
st[a][b] = true;
// bfs(a, b);
}
bfs();
long long sum = 0;
for(int i = 0; i<k; i++)
{
int x = buyer[i].first; int y = buyer[i].second.first; int c = buyer[i].second.second;
// cout<<x<<" "<<y<<" "<<endl;
sum += mincost[x][y] * c;
}
cout<<sum<<endl;
}
网友评论