3 2 1 2 5 6 2 3 4 5 1 3 0 0
9 11
這題輸入的時候要特別注意呀,首先題目並沒有說有重邊,但是不判斷重邊會WA,還有就是先要保證在最短路經的情況下心求花費,而不是兩個孤立的求,所以輸入的時候要特別注意,這是到水的很有意義的題目
#include#include #include #include #include #include #include #include using namespace std; #define maxn 1210 #define LL int const LL INF = 1e9+1000; int mp[maxn][maxn]; int money[maxn][maxn]; LL dis[maxn]; LL mny[maxn]; int n,m; void init() { for(int i = 1; i <= n; i++) fill(money[i],money[i]+n+1,INF), fill(mp[i],mp[i]+n+1,INF); fill(dis,dis+n+1,INF); fill(mny,mny+n+1,INF); } void SPFA(int s) { queue Q; dis[s] = 0; mny[s] = 0; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = 1;i <= n; i++) { if(dis[i] > mp[u][i] + dis[u]) { dis[i] = mp[u][i] + dis[u]; mny[i] = money[u][i] + mny[u]; Q.push(i); } else if(dis[i] == mp[u][i] + dis[u]) { mny[i] = min(mny[i],money[u][i] + mny[u]); } } } } int main() { #ifdef xxz freopen("in.txt","r",stdin); #endif // xxz while(~scanf("%d%d",&n,&m)) { if( n == 0 && m == 0) break; init(); for(int i = 1; i <= m; i++) { int a,b ,c, d; scanf("%d%d%d%d",&a,&b,&c,&d); if(c < mp[a][b]) { mp[a][b] = mp[b][a] = c; money[a][b] = money[b][a] = d; } else if(c == mp[a][b]) { if(d < money[a][b]) money[a][b] = money[b][a] = d; } } int st,ed; scanf("%d%d",&st,&ed); SPFA(st); printf("%d %d\n",dis[ed],mny[ed]); } return 0; }