Yaroslav and Time CodeForces - 302D
考察:最短路
思路:
就是最短路....由贪心知道去曾今去过的点只会平白无故损失能量,所以要尽量不走旧点,可以跑一遍dijkstra
g[n][n]需要long long,不知道是不是因为这个反复WA....
1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 typedef long long LL; 6 typedef pair<int,int> PII; 7 const int N = 110; 8 const LL INF = 0x3f3f3f3f3f3f3f3f; 9 int n,d,g[N][N]; 10 LL dist[N]; 11 bool st[N]; 12 struct Node{ 13 int x,y,w; 14 }node[N]; 15 LL dijkstra() 16 { 17 for(int i=1;i<=n;i++) dist[i] = (1ll<<63)-1; 18 dist[1] = 0; 19 priority_queue<PII,vector<PII>,greater<PII> > q; 20 q.push({0,1}); 21 while(q.size()) 22 { 23 PII it = q.top(); 24 q.pop(); 25 int u = it.second; 26 if(st[u]) continue; 27 st[u] = 1; 28 for(int i=1;i<=n;i++) 29 { 30 if(u!=i&&!st[i]&&dist[i]>dist[u]+g[u][i]) 31 { 32 dist[i] = dist[u]+g[u][i]; 33 q.push({dist[i],i}); 34 } 35 } 36 } 37 return dist[n]; 38 } 39 int main() 40 { 41 scanf("%d%d",&n,&d); 42 for(int i=2;i<n;i++) scanf("%d",&node[i].w); 43 for(int i=1;i<=n;i++) scanf("%d%d",&node[i].x,&node[i].y); 44 for(int i=1;i<=n;i++) 45 for(int j=1;j<=n;j++) 46 if(i!=j) 47 g[i][j] = d*(abs(node[i].x-node[j].x)+abs(node[i].y-node[j].y))-node[j].w; 48 else g[i][j] = INF; 49 printf("%lld\n",dijkstra()); 50 return 0; 51 }