CodeForcesGym 100753A A Journey to Greece

A Journey to Greece

Time Limit: 5000ms

Memory Limit: 262144KB

This problem will be judged on CodeForcesGym. Original ID: 100753A
64-bit integer IO format: %I64d      Java class name: (Any)

解题:状压dp

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int,int> pii;
 4 const int maxn = 20010;
 5 struct arc {
 6     int to,cost,next;
 7     arc(int x = 0,int y = 0,int z = -1) {
 8         to = x;
 9         cost = y;
10         next = z;
11     }
12 } e[maxn*50];
13 int head[maxn],hs[maxn],tot,N,P,M,G,T;
14 void add(int u,int v,int cost) {
15     e[tot] = arc(v,cost,head[u]);
16     head[u] = tot++;
17 }
18 priority_queue<pii,vector< pii >,greater< pii > >q;
19 int d[20][maxn];
20 bool done[maxn];
21 void dijkstra(int S) {
22     while(!q.empty()) q.pop();
23     int s = hs[S];
24     memset(d[s],0x3f,sizeof d[s]);
25     q.push(pii(d[s][S] = 0,S));
26     memset(done,false,sizeof done);
27     while(!q.empty()) {
28         int u = q.top().second;
29         q.pop();
30         if(done[u]) continue;
31         done[u] = true;
32         for(int i = head[u]; ~i; i = e[i].next) {
33             if(d[s][e[i].to] > d[s][u] + e[i].cost) {
34                 d[s][e[i].to] = d[s][u] + e[i].cost;
35                 q.push(pii(d[s][e[i].to],e[i].to));
36             }
37         }
38     }
39 }
40 int city[20],ttime[20],dp[1<<20][20][2];
41 int main() {
42     int u,v,w;
43     while(~scanf("%d%d%d%d%d",&N,&P,&M,&G,&T)) {
44         memset(head,-1,sizeof head);
45         bool zero = false;
46         for(int i = tot = 0; i < P; ++i) {
47             scanf("%d%d",city + i,ttime + i);
48             hs[city[i]] = i;
49             if(city[i] == 0) zero = true;
50         }
51         for(int i = 0; i < M; ++i) {
52             scanf("%d%d%d",&u,&v,&w);
53             add(u,v,w);
54             add(v,u,w);
55         }
56         for(int i = 0; i < P; ++i) dijkstra(city[i]);
57         if(!zero){
58             hs[0] = P;
59             dijkstra(0);
60         }
61         memset(dp,0x3f,sizeof dp);
62         for(int i = 0; i < P; ++i){
63             dp[1<<i][i][1] = d[hs[0]][city[i]] + ttime[i];
64             dp[1<<i][i][0] = T + ttime[i];
65         }
66         int st = (1<<P);
67         for(int i = 1; i < st; ++i){
68             for(int j = 0; j < P; ++j){
69                 if(((i>>j)&1) == 0) continue;
70                 for(int k = 0; k < P; ++k){
71                     if((i>>k)&1) continue;
72                     dp[i|(1<<k)][k][1] = min(dp[i|(1<<k)][k][1],dp[i][j][1] + d[j][city[k]] + ttime[k]);
73                     dp[i|(1<<k)][k][0] = min(dp[i|(1<<k)][k][0],dp[i][j][1] + T + ttime[k]);
74                     dp[i|(1<<k)][k][0] = min(dp[i|(1<<k)][k][0],dp[i][j][0] + d[j][city[k]] + ttime[k]);
75                 }
76             }
77         }
78         bool wtx = false,poss = false;
79         for(int i = 0; i < P && !wtx; ++i){
80             if(dp[st-1][i][1] + d[i][0] <= G) wtx = true;
81             if(dp[st-1][i][1] + T <= G) poss = true;
82             if(dp[st-1][i][0] + d[i][0] <= G) poss = true;
83         }
84         if(wtx) puts("possible without taxi");
85         else if(poss) puts("possible with taxi");
86         else puts("impossible");
87     }
88     return 0;
89 }

时间: 2024-10-26 07:21:12

CodeForcesGym 100753A A Journey to Greece的相关文章

CodeForcesGym 100502K Train Passengers

Train Passengers Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Original ID: 100502K64-bit integer IO format: %I64d      Java class name: (Any) The Nordic Company of Passing Carriages is losing money at an ala

A Knight&#39;s Journey(DFS)深搜

A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33634 Accepted: 11450 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around

POJ2488A Knight&#39;s Journey(dfs+数学)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32422   Accepted: 11040 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

POJ 2488 A Knight&#39;s Journey

A Knight's Journey Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 248864-bit integer IO format: %lld      Java class name: Main Background  The knight is getting bored of seeing the same black and white squa

Codeforces 789D Weird journey - 欧拉路 - 图论

Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland — Uzhlyandia. It is widely known that Uzhlyandia has n cities connected with m bidirectional roads. Also, there are no two roads in the countr

I - A Knight&#39;s Journey

I - A Knight's Journey Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description BackgroundThe knight is getting bored of seeing the same black and white squares again and again and has decided to make a j

poj_2488 A Knight&#39;s Journey

比较典型的深搜,注意1,最后的输出格式,我imposiable后面忘记endl了,结果PE了两次,有点可惜: 2.最后要以字典序输出 1 #include<iostream> 2 #include<string.h> 3 using namespace std; 4 5 #define MAX 8 6 7 int p,q; 8 int board[MAX][MAX]; 9 int steps[MAX*MAX]; 10 11 int dir[8][2] = {{-1,-2},{1,-

POJ2488-A Knight&#39;s Journey(DFS+回溯)

题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36695   Accepted: 12462 Description Background The knight is getting bored of seeing the same black and white squares again and again

CodeForcesGym 100212E Long Dominoes

Long Dominoes Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. Original ID: 100212E64-bit integer IO format: %I64d      Java class name: (Any) Find the number of ways to tile an m*n rectangle with long dominoes -