标签:
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 }
CodeForcesGym 100753A A Journey to Greece
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4856026.html