题目链接: http://poj.org/problem?id=3635
题目大意及分析:
http://hi.baidu.com/legend_ni/blog/item/c7525409cf8057f036d1229d.html
http://ip96cns.blog.163.com/blog/static/1700951922011215104442567/
显然这题我不会,后来觉得应该要dp的才是,因为前后总是会有影响。
看了解题报告后似乎有点明白,然后自己实现了代码,发现比上面链接里的代码略了一个数组dp[][],我的代码还是比他们的更有可取之处的,我想我以前还是不曾深入地认识bfs+优先队列。
另外能把100个容量分成1个个来作为状态进行bfs也是需要勇气和经验的。
注意代码里面 vis[][]=1 的赋值必须是在出队列的时候才可以,否则按我之前的一个写法(见下面第二个代码)是错误的。
AC代码:
poj3635
1 /*3635 Accepted 1092K 391MS C++ 2041B 2012-06-25 22:35:55*/ 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <iostream> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 using namespace std; 10 11 #define mpair make_pair 12 #define pii pair<int,int> 13 #define MM(a,b) memset(a,b,sizeof(a)); 14 typedef long long lld; 15 typedef unsigned long long u64; 16 template<class T> bool up_max(T& a,const T& b){return b>a? a=b,1 : 0;} 17 template<class T> bool up_min(T& a,const T& b){return b<a? a=b,1 : 0;} 18 #define maxn 1010 19 20 int n,m; 21 int p[maxn]; 22 vector< pii > map[maxn]; 23 24 struct Node{ 25 int u, vol, cost; /// u: cur node; 26 Node(int u_,int vol_,int cost_){u=u_,vol=vol_,cost=cost_;} 27 bool operator<(Node a)const{ 28 return cost > a.cost; /// 29 } 30 }; 31 priority_queue<Node> Q; 32 33 bool vis[maxn][105]; 34 int bfs(int VOL, int st, int ed){ 35 MM( vis, 0 ); 36 while( !Q.empty() ) Q.pop(); 37 Q.push( Node( st, 0, 0 ) ); 38 while( !Q.empty() ){ 39 Node a= Q.top(); Q.pop(); 40 int u= a.u, vol= a.vol, cost= a.cost; 41 vis[u][vol]= 1; 42 if( u==ed ) return cost; 43 44 /// we choose to add a unit fuel; 45 if( vol<VOL && !vis[u][vol+1] ) 46 Q.push( Node( u, vol+1, cost+p[u] ) ); 47 48 /// we choose to go to the next node; 49 for(int i=0;i<map[u].size();++i){ 50 int v= map[u][i].first, w= map[u][i].second; 51 if( vol>=w && !vis[v][vol-w] ) 52 Q.push( Node( v, vol-w, cost ) ); 53 } 54 } 55 return -1; 56 } 57 58 int main() 59 { 60 //freopen("poj3635.in","r",stdin); 61 while( cin>>n>>m ){ 62 int i,u,v,x,q; 63 for(i=0;i<n;++i) scanf("%d", p+i), map[i].clear(); 64 while( m-- ){ 65 scanf("%d%d%d", &u,&v,&x); 66 map[u].push_back( pii( v, x ) ); 67 map[v].push_back( pii( u, x ) ); 68 } 69 70 cin>>q; 71 while( q-- ){ 72 scanf("%d%d%d", &x, &u, &v); 73 int ans= bfs(x, u, v); 74 if( -1==ans ) puts("impossible"); 75 else printf("%d\n", ans); 76 } 77 } 78 }
WA代码:
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 using namespace std; 9 10 #define mpair make_pair 11 #define pii pair<int,int> 12 #define MM(a,b) memset(a,b,sizeof(a)); 13 typedef long long lld; 14 typedef unsigned long long u64; 15 template<class T> bool up_max(T& a,const T& b){return b>a? a=b,1 : 0;} 16 template<class T> bool up_min(T& a,const T& b){return b<a? a=b,1 : 0;} 17 #define maxn 1010 18 19 int n,m; 20 int p[maxn]; 21 vector< pii > map[maxn]; 22 23 struct Node{ 24 int u, vol, cost; /// u: cur node; 25 Node(int u_,int vol_,int cost_){u=u_,vol=vol_,cost=cost_;} 26 bool operator<(Node a)const{ 27 return cost > a.cost; /// 28 } 29 }; 30 priority_queue<Node> Q; 31 32 bool vis[maxn][105]; 33 int bfs(int VOL, int st, int ed){ 34 MM( vis, 0 ); 35 while( !Q.empty() ) Q.pop(); 36 Q.push( Node( st, 0, 0 ) ); 37 vis[st][0]= 1; 38 while( !Q.empty() ){ 39 Node a= Q.top(); Q.pop(); 40 int u= a.u, vol= a.vol, cost= a.cost; 41 if( u==ed ) return cost; 42 43 /// we choose to add a unit fuel; 44 if( vol<VOL && !vis[u][vol+1] ){ 45 Q.push( Node( u, vol+1, cost+p[u] ) ); 46 vis[u][vol+1]= 1; 47 } 48 49 /// we choose to go to the next node; 50 for(int i=0;i<map[u].size();++i){ 51 int v= map[u][i].first, w= map[u][i].second; 52 if( vol>=w && !vis[v][vol-w] ){ 53 Q.push( Node( v, vol-w, cost ) ); 54 vis[v][vol-w]= 1; 55 } 56 } 57 } 58 return -1; 59 } 60 61 int main() 62 { 63 //freopen("poj3635.in","r",stdin); 64 while( cin>>n>>m ){ 65 int i,u,v,x,q; 66 for(i=0;i<n;++i) scanf("%d", p+i), map[i].clear(); 67 while( m-- ){ 68 scanf("%d%d%d", &u,&v,&x); 69 map[u].push_back( pii( v, x ) ); 70 map[v].push_back( pii( u, x ) ); 71 } 72 73 cin>>q; 74 while( q-- ){ 75 scanf("%d%d%d", &x, &u, &v); 76 int ans= bfs(x, u, v); 77 if( -1==ans ) puts("impossible"); 78 else printf("%d\n", ans); 79 } 80 } 81 }